b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1 | /* //device/system/reference-ril/atchannel.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 "atchannel.h" |
| 19 | #include "at_tok.h" |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <pthread.h> |
| 24 | #include <ctype.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <errno.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <time.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #include "mbtk_log.h" |
| 33 | #include "mbtk_type.h" |
| 34 | #include "mbtk_utils.h" |
| 35 | |
| 36 | #define MAX_AT_RESPONSE (8 * 1024) |
| 37 | #define HANDSHAKE_RETRY_COUNT 20 |
| 38 | #define HANDSHAKE_TIMEOUT_MSEC 500 |
b.liu | 15f456b | 2024-10-31 20:16:06 +0800 | [diff] [blame] | 39 | #define AT_BUFF_MAX 1024 |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 40 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 41 | static pthread_t s_tid_reader[ATPORTID_NUM]; |
| 42 | static int s_at_fd[ATPORTID_NUM] = {-1}; /* fd of the AT channel */ |
| 43 | static int s_uart_fd[MBTK_SIM_NUM] = {-1}; /* fd of the UART channel */ |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 44 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 45 | static ATUnsolHandler s_unsolHandler[MBTK_SIM_NUM]; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 46 | |
| 47 | /* for input buffering */ |
| 48 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 49 | static char s_ATBuffer[ATPORTID_NUM][MAX_AT_RESPONSE+1]; |
| 50 | static char *s_ATBufferCur[ATPORTID_NUM] = {s_ATBuffer[ATPORTID_SIM1_0], s_ATBuffer[ATPORTID_SIM1_0], s_ATBuffer[ATPORTID_SIM1_0], |
| 51 | s_ATBuffer[ATPORTID_SIM2_0], s_ATBuffer[ATPORTID_SIM2_0], s_ATBuffer[ATPORTID_SIM2_0]}; |
| 52 | static char s_UartBuffer[MBTK_SIM_NUM][MAX_AT_RESPONSE+1]; |
| 53 | static char *s_UartBufferCur[MBTK_SIM_NUM] = {s_UartBuffer[MBTK_SIM_1], s_UartBuffer[MBTK_SIM_2]}; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 54 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 55 | static mbtk_ril_at_state_enum at_state[ATPORTID_NUM] = {RIL_AT_STATE_CLOSED}; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 56 | |
| 57 | #if AT_DEBUG |
| 58 | void AT_DUMP(const char* prefix, const char* buff, int len) |
| 59 | { |
| 60 | if (len < 0) |
| 61 | len = strlen(buff); |
| 62 | LOGD("%.*s", len, buff); |
| 63 | } |
| 64 | #endif |
| 65 | |
| 66 | /* |
| 67 | * There is one reader thread |s_tid_reader| and potentially multiple writer |
| 68 | * threads. |s_commandmutex| and |s_commandcond| are used to maintain the |
| 69 | * condition that the writer thread will not read from |sp_response| until the |
| 70 | * reader thread has signaled itself is finished, etc. |s_writeMutex| is used to |
| 71 | * prevent multiple writer threads from calling at_send_command_full_nolock |
| 72 | * function at the same time. |
| 73 | */ |
| 74 | |
| 75 | // "Wait" when AT process... |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 76 | static pthread_mutex_t s_commandmutex[ATPORTID_NUM] = {PTHREAD_MUTEX_INITIALIZER}; |
| 77 | static pthread_cond_t s_commandcond[ATPORTID_NUM] = {PTHREAD_COND_INITIALIZER}; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 78 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 79 | static ATCommandType s_type[ATPORTID_NUM]; |
| 80 | static const char *s_responsePrefix[ATPORTID_NUM] = {NULL}; |
| 81 | static const char *s_smsPDU[ATPORTID_NUM] = {NULL}; |
| 82 | static ATResponse *sp_response[ATPORTID_NUM] = {NULL}; |
| 83 | static char s_curr_at[ATPORTID_NUM][AT_BUFF_MAX]; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 84 | |
| 85 | static void (*s_onTimeout)(void) = NULL; |
| 86 | static void (*s_onReaderClosed)(void) = NULL; |
| 87 | static int s_readerClosed; |
| 88 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 89 | static void onReaderClosed(ATPortId_enum port); |
| 90 | static int writeCtrlZ (ATPortId_enum port, const char *s); |
| 91 | static int writeline (ATPortId_enum port, const char *s); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 92 | |
| 93 | typedef struct |
| 94 | { |
| 95 | char *at_command; |
| 96 | long long timeout; // ms |
| 97 | bool timeout_close; // Close AT or not while AT response timeout. |
| 98 | } at_timeout_t; |
| 99 | |
| 100 | static at_timeout_t at_timeout_list[] = |
| 101 | { |
| 102 | {"AT+CRSM", 10000, false}, |
| 103 | // {"AT+COPS", 60000, false} |
| 104 | }; |
| 105 | |
| 106 | #define NS_PER_S 1000000000 |
| 107 | static void setTimespecRelative(struct timespec *p_ts, long long msec) |
| 108 | { |
| 109 | struct timeval tv; |
| 110 | |
| 111 | gettimeofday(&tv, (struct timezone *) NULL); |
| 112 | |
| 113 | p_ts->tv_sec = tv.tv_sec + (msec / 1000); |
| 114 | p_ts->tv_nsec = (tv.tv_usec + (msec % 1000) * 1000L ) * 1000L; |
| 115 | /* assuming tv.tv_usec < 10^6 */ |
| 116 | if (p_ts->tv_nsec >= NS_PER_S) |
| 117 | { |
| 118 | p_ts->tv_sec++; |
| 119 | p_ts->tv_nsec -= NS_PER_S; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | static void sleepMsec(long long msec) |
| 124 | { |
| 125 | struct timespec ts; |
| 126 | int err; |
| 127 | |
| 128 | ts.tv_sec = (msec / 1000); |
| 129 | ts.tv_nsec = (msec % 1000) * 1000 * 1000; |
| 130 | |
| 131 | do |
| 132 | { |
| 133 | err = nanosleep (&ts, &ts); |
| 134 | } |
| 135 | while (err < 0 && errno == EINTR); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | |
| 140 | /** add an intermediate response to sp_response*/ |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 141 | static void addIntermediate(ATPortId_enum port, const char *line) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 142 | { |
| 143 | ATLine *p_new; |
| 144 | |
| 145 | p_new = (ATLine *) malloc(sizeof(ATLine)); |
| 146 | |
| 147 | p_new->line = strdup(line); |
| 148 | |
| 149 | // LOGD("line:%s", line); |
| 150 | // LOGD("line-1:%s", p_new->line); |
| 151 | |
| 152 | /* note: this adds to the head of the list, so the list |
| 153 | will be in reverse order of lines received. the order is flipped |
| 154 | again before passing on to the command issuer */ |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 155 | p_new->p_next = sp_response[port]->p_intermediates; |
| 156 | sp_response[port]->p_intermediates = p_new; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
| 160 | /** |
| 161 | * returns 1 if line is a final response indicating error |
| 162 | * See 27.007 annex B |
| 163 | * WARNING: NO CARRIER and others are sometimes unsolicited |
| 164 | */ |
| 165 | static const char * s_finalResponsesError[] = |
| 166 | { |
| 167 | "ERROR", |
| 168 | "+CMS ERROR:", |
| 169 | "+CME ERROR:", |
| 170 | // "NO CARRIER", /* sometimes! */ // Only for ATD ? |
| 171 | "NO ANSWER", |
| 172 | "NO DIALTONE", |
| 173 | }; |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 174 | static int isFinalResponseError(ATPortId_enum port, const char *line) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 175 | { |
| 176 | size_t i; |
| 177 | |
| 178 | for (i = 0 ; i < ARRAY_SIZE(s_finalResponsesError) ; i++) |
| 179 | { |
| 180 | if (strStartsWith(line, s_finalResponsesError[i])) |
| 181 | { |
| 182 | return 1; |
| 183 | } |
| 184 | } |
| 185 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 186 | if(!strncasecmp(s_curr_at[port], "ATD", 3) && strStartsWith(line, "NO CARRIER")) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 187 | { |
| 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * returns 1 if line is a final response indicating success |
| 196 | * See 27.007 annex B |
| 197 | * WARNING: NO CARRIER and others are sometimes unsolicited |
| 198 | */ |
| 199 | static const char * s_finalResponsesSuccess[] = |
| 200 | { |
| 201 | "OK", |
| 202 | // "CONNECT" /* some stacks start up data on another channel */ |
| 203 | }; |
| 204 | static int isFinalResponseSuccess(const char *line) |
| 205 | { |
| 206 | size_t i; |
| 207 | |
| 208 | for (i = 0 ; i < ARRAY_SIZE(s_finalResponsesSuccess) ; i++) |
| 209 | { |
| 210 | if (strStartsWith(line, s_finalResponsesSuccess[i])) |
| 211 | { |
| 212 | return 1; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * returns 1 if line is a final response, either error or success |
| 221 | * See 27.007 annex B |
| 222 | * WARNING: NO CARRIER and others are sometimes unsolicited |
| 223 | */ |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 224 | static int isFinalResponse(ATPortId_enum port, const char *line) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 225 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 226 | return isFinalResponseSuccess(line) || isFinalResponseError(port, line); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
| 230 | * returns 1 if line is the first line in (what will be) a two-line |
| 231 | * SMS unsolicited response |
| 232 | */ |
| 233 | static const char * s_smsUnsoliciteds[] = |
| 234 | { |
| 235 | "+CMT:", |
| 236 | "+CDS:", |
| 237 | "+CBM:" |
| 238 | }; |
| 239 | static int isSMSUnsolicited(const char *line) |
| 240 | { |
| 241 | size_t i; |
| 242 | |
| 243 | for (i = 0 ; i < ARRAY_SIZE(s_smsUnsoliciteds) ; i++) |
| 244 | { |
| 245 | if (strStartsWith(line, s_smsUnsoliciteds[i])) |
| 246 | { |
| 247 | return 1; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /** assumes s_commandmutex is held */ |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 256 | static void handleFinalResponse(ATPortId_enum port, const char *line) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 257 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 258 | sp_response[port]->finalResponse = strdup(line); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 259 | |
| 260 | //LOGD("AT complete (pthread_cond_signal): %s",line); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 261 | pthread_cond_signal(&s_commandcond[port]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 262 | } |
| 263 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 264 | static void handleUnsolicited(ATPortId_enum port, const char *line) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 265 | { |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 266 | if (s_unsolHandler[port < ATPORTID_SIM2_0 ? MBTK_SIM_1 : MBTK_SIM_2] != NULL) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 267 | { |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 268 | s_unsolHandler[port < ATPORTID_SIM2_0 ? MBTK_SIM_1 : MBTK_SIM_2](line, NULL); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 272 | static void processLine(ATPortId_enum port, const char *line) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 273 | { |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 274 | LOGD("lynq read process begin process line port %d %s", port,line); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 275 | pthread_mutex_lock(&s_commandmutex[port]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 276 | // LOGD("LINE : %s", line); |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 277 | LOGD("lynq read process get lock port %d", port); |
| 278 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 279 | if (sp_response[port] == NULL) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 280 | { |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 281 | LOGD("lynq read 2 port %d", port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 282 | /* no command pending */ |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 283 | handleUnsolicited(port, line); |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 284 | |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 285 | } |
| 286 | else if (isFinalResponseSuccess(line)) |
| 287 | { |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 288 | LOGD("lynq read 3 port %d", port); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 289 | sp_response[port]->success = 1; |
| 290 | handleFinalResponse(port, line); |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 291 | |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 292 | } |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 293 | else if (isFinalResponseError(port, line)) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 294 | { |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 295 | LOGD("lynq read 4 port %d", port); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 296 | sp_response[port]->success = 0; |
| 297 | handleFinalResponse(port, line); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 298 | } |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 299 | else if (s_smsPDU[port] != NULL && 0 == strcmp(line, "> ")) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 300 | { |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 301 | LOGD("lynq read 5 port %d", port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 302 | // See eg. TS 27.005 4.3 |
| 303 | // Commands like AT+CMGS have a "> " prompt |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 304 | writeCtrlZ(port, s_smsPDU[port]); |
| 305 | s_smsPDU[port] = NULL; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 306 | } |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 307 | else switch (s_type[port]) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 308 | { |
| 309 | case NO_RESULT: |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 310 | LOGD("lynq read 6 port %d", port); |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 311 | handleUnsolicited(port, line); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 312 | break; |
| 313 | case NUMERIC: |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 314 | LOGD("lynq read 7 port %d", port); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 315 | if (sp_response[port]->p_intermediates == NULL |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 316 | && isdigit(line[0]) |
| 317 | ) |
| 318 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 319 | addIntermediate(port, line); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 320 | } |
| 321 | else |
| 322 | { |
| 323 | /* either we already have an intermediate response or |
| 324 | the line doesn't begin with a digit */ |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 325 | handleUnsolicited(port, line); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 326 | } |
| 327 | break; |
| 328 | case SINGLELINE: |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 329 | LOGD("lynq read 8 port %d", port); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 330 | if (sp_response[port]->p_intermediates == NULL |
| 331 | && strStartsWith (line, s_responsePrefix[port]) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 332 | ) |
| 333 | { |
| 334 | if(*line == '"') |
| 335 | { |
| 336 | char *line_temp = strdup(line); |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame] | 337 | char *free_ptr = line_temp; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 338 | line_temp++; |
| 339 | if(strlen(line_temp) > 0) |
| 340 | { |
| 341 | char *ptr = line_temp + strlen(line_temp) - 1; |
| 342 | while(ptr >= line_temp && *ptr == '"') |
| 343 | { |
| 344 | *ptr = '\0'; |
| 345 | ptr--; |
| 346 | } |
| 347 | } |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 348 | addIntermediate(port, line_temp); |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame] | 349 | free(free_ptr); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 350 | } |
| 351 | else |
| 352 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 353 | addIntermediate(port, line); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | /* we already have an intermediate response */ |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 359 | handleUnsolicited(port, line); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 360 | } |
| 361 | break; |
| 362 | case MULTILINE: |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 363 | LOGD("lynq read 9 port %d", port); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 364 | if (strStartsWith (line, s_responsePrefix[port])) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 365 | { |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 366 | LOGD("lynq read 10 port %d", port); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 367 | addIntermediate(port, line); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 368 | } |
| 369 | else |
| 370 | { |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 371 | handleUnsolicited(port, line); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 372 | } |
| 373 | break; |
| 374 | |
| 375 | default: /* this should never be reached */ |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 376 | LOGD("lynq read 11 port %d", port); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 377 | LOGE("Unsupported AT command type %d\n", s_type[port]); |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 378 | handleUnsolicited(port, line); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 379 | break; |
| 380 | } |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 381 | LOGD("lynq read 12 port %d", port); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 382 | pthread_mutex_unlock(&s_commandmutex[port]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | |
| 386 | /** |
| 387 | * Returns a pointer to the end of the next line |
| 388 | * special-cases the "> " SMS prompt |
| 389 | * |
| 390 | * returns NULL if there is no complete line |
| 391 | */ |
| 392 | static char * findNextEOL(char *cur) |
| 393 | { |
| 394 | if (cur[0] == '>' && cur[1] == ' ' && cur[2] == '\0') |
| 395 | { |
| 396 | /* SMS prompt character...not \r terminated */ |
| 397 | return cur+2; |
| 398 | } |
| 399 | |
| 400 | // Find next newline |
| 401 | while (*cur != '\0' && *cur != '\r' && *cur != '\n') cur++; |
| 402 | |
| 403 | return *cur == '\0' ? NULL : cur; |
| 404 | } |
| 405 | |
| 406 | |
| 407 | /** |
| 408 | * Reads a line from the AT channel, returns NULL on timeout. |
| 409 | * Assumes it has exclusive read access to the FD |
| 410 | * |
| 411 | * This line is valid only until the next call to readline |
| 412 | * |
| 413 | * This function exists because as of writing, android libc does not |
| 414 | * have buffered stdio. |
| 415 | */ |
| 416 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 417 | static const char *readline(ATPortId_enum port) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 418 | { |
| 419 | ssize_t count; |
| 420 | |
| 421 | char *p_read = NULL; |
| 422 | char *p_eol = NULL; |
| 423 | char *ret; |
| 424 | |
| 425 | /* this is a little odd. I use *s_ATBufferCur == 0 to |
| 426 | * mean "buffer consumed completely". If it points to a character, than |
| 427 | * the buffer continues until a \0 |
| 428 | */ |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 429 | if (*s_ATBufferCur[port] == '\0') |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 430 | { |
| 431 | /* empty buffer */ |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 432 | s_ATBufferCur[port] = s_ATBuffer[port]; |
| 433 | *s_ATBufferCur[port] = '\0'; |
| 434 | p_read = s_ATBuffer[port]; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 435 | } |
| 436 | else /* *s_ATBufferCur != '\0' */ |
| 437 | { |
| 438 | /* there's data in the buffer from the last read */ |
| 439 | |
| 440 | // skip over leading newlines |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 441 | while (*s_ATBufferCur[port] == '\r' || *s_ATBufferCur[port] == '\n') |
| 442 | s_ATBufferCur[port]++; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 443 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 444 | p_eol = findNextEOL(s_ATBufferCur[port]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 445 | |
| 446 | if (p_eol == NULL) |
| 447 | { |
| 448 | /* a partial line. move it up and prepare to read more */ |
| 449 | size_t len; |
| 450 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 451 | len = strlen(s_ATBufferCur[port]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 452 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 453 | memmove(s_ATBuffer[port], s_ATBufferCur[port], len + 1); |
| 454 | p_read = s_ATBuffer[port] + len; |
| 455 | s_ATBufferCur[port] = s_ATBuffer[port]; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 456 | } |
| 457 | /* Otherwise, (p_eol !- NULL) there is a complete line */ |
| 458 | /* that will be returned the while () loop below */ |
| 459 | } |
| 460 | |
| 461 | while (p_eol == NULL) |
| 462 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 463 | if (0 == MAX_AT_RESPONSE - (p_read - s_ATBuffer[port])) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 464 | { |
| 465 | LOGE("ERROR: Input line exceeded buffer\n"); |
| 466 | /* ditch buffer and start over again */ |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 467 | s_ATBufferCur[port] = s_ATBuffer[port]; |
| 468 | *s_ATBufferCur[port] = '\0'; |
| 469 | p_read = s_ATBuffer[port]; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | do |
| 473 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 474 | count = read(s_at_fd[port], p_read, |
| 475 | MAX_AT_RESPONSE - (p_read - s_ATBuffer[port])); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 476 | usleep(10000); |
| 477 | } |
| 478 | while (count < 0 && errno == EINTR); |
| 479 | |
| 480 | if (count > 0) |
| 481 | { |
| 482 | AT_DUMP( "<< ", p_read, count ); |
| 483 | |
| 484 | p_read[count] = '\0'; |
| 485 | |
| 486 | // skip over leading newlines |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 487 | while (*s_ATBufferCur[port] == '\r' || *s_ATBufferCur[port] == '\n') |
| 488 | s_ATBufferCur[port]++; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 489 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 490 | p_eol = findNextEOL(s_ATBufferCur[port]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 491 | p_read += count; |
| 492 | } |
| 493 | else if (count <= 0) |
| 494 | { |
| 495 | /* read error encountered or EOF reached */ |
| 496 | if(count == 0) |
| 497 | { |
b.liu | fd87baf | 2024-11-15 15:30:38 +0800 | [diff] [blame] | 498 | LOGD("atchannel[Port-%d]: EOF reached", port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 499 | } |
| 500 | else |
| 501 | { |
b.liu | fd87baf | 2024-11-15 15:30:38 +0800 | [diff] [blame] | 502 | LOGD("atchannel[Port-%d]: read error %s", port, strerror(errno)); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 503 | } |
| 504 | return NULL; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /* a full line in the buffer. Place a \0 over the \r and return */ |
| 509 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 510 | ret = s_ATBufferCur[port]; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 511 | *p_eol = '\0'; |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 512 | s_ATBufferCur[port] = p_eol + 1; /* this will always be <= p_read, */ |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 513 | /* and there will be a \0 at *p_read */ |
| 514 | |
b.liu | fd87baf | 2024-11-15 15:30:38 +0800 | [diff] [blame] | 515 | LOGD("[Port-%d]AT< %s", port, ret); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 516 | return ret; |
| 517 | } |
| 518 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 519 | static const char *readlineUrc(ATPortId_enum port) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 520 | { |
| 521 | ssize_t count; |
| 522 | |
| 523 | char *p_read = NULL; |
| 524 | char *p_eol = NULL; |
| 525 | char *ret; |
| 526 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 527 | mbtk_sim_type_enum sim_id = port < ATPORTID_SIM2_0 ? MBTK_SIM_1 : MBTK_SIM_2; |
| 528 | |
| 529 | |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 530 | /* this is a little odd. I use *s_ATBufferCur == 0 to |
| 531 | * mean "buffer consumed completely". If it points to a character, than |
| 532 | * the buffer continues until a \0 |
| 533 | */ |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 534 | if (*s_UartBufferCur[sim_id] == '\0') |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 535 | { |
| 536 | /* empty buffer */ |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 537 | s_UartBufferCur[sim_id] = s_UartBuffer[sim_id]; |
| 538 | *s_UartBufferCur[sim_id] = '\0'; |
| 539 | p_read = s_UartBuffer[sim_id]; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 540 | } |
| 541 | else /* *s_ATBufferCur != '\0' */ |
| 542 | { |
| 543 | /* there's data in the buffer from the last read */ |
| 544 | |
| 545 | // skip over leading newlines |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 546 | while (*s_UartBufferCur[sim_id] == '\r' || *s_UartBufferCur[sim_id] == '\n') |
| 547 | s_UartBufferCur[sim_id]++; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 548 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 549 | p_eol = findNextEOL(s_UartBufferCur[sim_id]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 550 | |
| 551 | if (p_eol == NULL) |
| 552 | { |
| 553 | /* a partial line. move it up and prepare to read more */ |
| 554 | size_t len; |
| 555 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 556 | len = strlen(s_UartBufferCur[sim_id]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 557 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 558 | memmove(s_UartBuffer[sim_id], s_UartBufferCur[sim_id], len + 1); |
| 559 | p_read = s_UartBuffer[sim_id] + len; |
| 560 | s_UartBufferCur[sim_id] = s_UartBuffer[sim_id]; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 561 | } |
| 562 | /* Otherwise, (p_eol !- NULL) there is a complete line */ |
| 563 | /* that will be returned the while () loop below */ |
| 564 | } |
| 565 | |
| 566 | while (p_eol == NULL) |
| 567 | { |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 568 | if (0 == MAX_AT_RESPONSE - (p_read - s_UartBuffer[sim_id])) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 569 | { |
| 570 | LOGE("ERROR: Input line exceeded buffer\n"); |
| 571 | /* ditch buffer and start over again */ |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 572 | s_UartBufferCur[sim_id] = s_UartBuffer[sim_id]; |
| 573 | *s_UartBufferCur[sim_id] = '\0'; |
| 574 | p_read = s_UartBuffer[sim_id]; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | do |
| 578 | { |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 579 | count = read(s_uart_fd[sim_id], p_read, |
| 580 | MAX_AT_RESPONSE - (p_read - s_UartBuffer[sim_id])); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 581 | usleep(10000); |
| 582 | } |
| 583 | while (count < 0 && errno == EINTR); |
| 584 | |
| 585 | if (count > 0) |
| 586 | { |
| 587 | AT_DUMP( "<< ", p_read, count ); |
| 588 | |
| 589 | p_read[count] = '\0'; |
| 590 | |
| 591 | // skip over leading newlines |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 592 | while (*s_UartBufferCur[sim_id] == '\r' || *s_UartBufferCur[sim_id] == '\n') |
| 593 | s_UartBufferCur[sim_id]++; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 594 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 595 | p_eol = findNextEOL(s_UartBufferCur[sim_id]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 596 | p_read += count; |
| 597 | } |
| 598 | else if (count <= 0) |
| 599 | { |
| 600 | /* read error encountered or EOF reached */ |
| 601 | if(count == 0) |
| 602 | { |
| 603 | LOGD("atchannel: EOF reached"); |
| 604 | } |
| 605 | else |
| 606 | { |
| 607 | LOGD("atchannel: read error %s", strerror(errno)); |
| 608 | } |
| 609 | return NULL; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | /* a full line in the buffer. Place a \0 over the \r and return */ |
| 614 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 615 | ret = s_UartBufferCur[sim_id]; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 616 | *p_eol = '\0'; |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 617 | s_UartBufferCur[sim_id] = p_eol + 1; /* this will always be <= p_read, */ |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 618 | /* and there will be a \0 at *p_read */ |
| 619 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 620 | LOGD("[Sim-%d,Port-%d]URC< %s", sim_id, port, ret); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 621 | return ret; |
| 622 | } |
| 623 | |
| 624 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 625 | static void onReaderClosed(ATPortId_enum port) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 626 | { |
| 627 | LOGD("onReaderClosed()"); |
| 628 | if (s_onReaderClosed != NULL && s_readerClosed == 0) |
| 629 | { |
| 630 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 631 | pthread_mutex_lock(&s_commandmutex[port]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 632 | |
| 633 | s_readerClosed = 1; |
| 634 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 635 | pthread_cond_signal(&s_commandcond[port]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 636 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 637 | pthread_mutex_unlock(&s_commandmutex[port]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 638 | |
| 639 | s_onReaderClosed(); |
| 640 | } |
| 641 | } |
| 642 | |
b.liu | 06559f6 | 2024-11-01 18:48:22 +0800 | [diff] [blame] | 643 | typedef struct |
| 644 | { |
| 645 | int cid; |
| 646 | bool act; |
| 647 | bool waitting; |
| 648 | } info_cgact_wait_t; |
| 649 | extern info_cgact_wait_t cgact_wait; |
| 650 | |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 651 | static void *readerLoop(void *arg) |
| 652 | { |
| 653 | UNUSED(arg); |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 654 | ATPortId_enum *port = (ATPortId_enum*)arg; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 655 | for (;;) |
| 656 | { |
| 657 | const char * line; |
| 658 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 659 | line = readline(*port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 660 | |
| 661 | if (line == NULL) |
| 662 | { |
b.liu | fd87baf | 2024-11-15 15:30:38 +0800 | [diff] [blame] | 663 | //usleep(50000); |
| 664 | //continue; |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 665 | LOGD("lynq read 1 port %d", *port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 666 | break; |
| 667 | } |
| 668 | |
| 669 | if(strStartsWith(line, "MBTK_AT_READY")) { |
| 670 | //handleUnsolicited(line); |
| 671 | continue; |
b.liu | 06559f6 | 2024-11-01 18:48:22 +0800 | [diff] [blame] | 672 | } else if(strStartsWith(line, "CONNECT")) { |
| 673 | if(cgact_wait.waitting && cgact_wait.act) { |
| 674 | cgact_wait.waitting = false; |
| 675 | } |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | if(isSMSUnsolicited(line)) |
| 679 | { |
| 680 | char *line1; |
| 681 | const char *line2; |
| 682 | |
| 683 | // The scope of string returned by 'readline()' is valid only |
| 684 | // till next call to 'readline()' hence making a copy of line |
| 685 | // before calling readline again. |
| 686 | line1 = strdup(line); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 687 | line2 = readline(*port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 688 | |
| 689 | if (line2 == NULL) |
| 690 | { |
| 691 | free(line1); |
| 692 | break; |
| 693 | } |
| 694 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 695 | if (s_unsolHandler[*port < ATPORTID_SIM2_0 ? MBTK_SIM_1 : MBTK_SIM_2] != NULL) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 696 | { |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 697 | s_unsolHandler[*port < ATPORTID_SIM2_0 ? MBTK_SIM_1 : MBTK_SIM_2] (line1, line2); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 698 | } |
| 699 | free(line1); |
| 700 | } |
| 701 | else |
| 702 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 703 | processLine(*port, line); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 707 | onReaderClosed(*port); |
| 708 | |
| 709 | free(port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 710 | |
| 711 | return NULL; |
| 712 | } |
| 713 | |
| 714 | static void *readerUrcLoop(void *arg) |
| 715 | { |
| 716 | UNUSED(arg); |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 717 | ATPortId_enum *port = (ATPortId_enum*)arg; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 718 | for (;;) |
| 719 | { |
| 720 | const char *line; |
| 721 | |
b.liu | fd87baf | 2024-11-15 15:30:38 +0800 | [diff] [blame] | 722 | line = readlineUrc(*port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 723 | |
| 724 | if (line == NULL) |
| 725 | { |
| 726 | break; |
| 727 | } |
| 728 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 729 | handleUnsolicited(*port, line); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 730 | } |
| 731 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 732 | onReaderClosed(*port); |
| 733 | |
| 734 | free(port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 735 | |
| 736 | return NULL; |
| 737 | } |
| 738 | |
| 739 | |
| 740 | /** |
| 741 | * Sends string s to the radio with a \r appended. |
| 742 | * Returns AT_ERROR_* on error, 0 on success |
| 743 | * |
| 744 | * This function exists because as of writing, android libc does not |
| 745 | * have buffered stdio. |
| 746 | */ |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 747 | static int writeline (ATPortId_enum port, const char *s) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 748 | { |
| 749 | size_t cur = 0; |
| 750 | size_t len = strlen(s); |
| 751 | ssize_t written; |
| 752 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 753 | if (s_at_fd[port] < 0 || s_readerClosed > 0) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 754 | { |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 755 | LOGE("lynq 12, port %d %d %d",port,s_at_fd[port],s_readerClosed); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 756 | return AT_ERROR_CHANNEL_CLOSED; |
| 757 | } |
| 758 | |
b.liu | fd87baf | 2024-11-15 15:30:38 +0800 | [diff] [blame] | 759 | LOGD("[Port-%d]AT> %s", port, s); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 760 | |
| 761 | AT_DUMP( ">> ", s, strlen(s) ); |
| 762 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 763 | memset(s_curr_at[port], 0x0, AT_BUFF_MAX); |
| 764 | memcpy(s_curr_at[port], s, strlen(s)); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 765 | |
| 766 | /* the main string */ |
| 767 | while (cur < len) |
| 768 | { |
| 769 | do |
| 770 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 771 | written = write (s_at_fd[port], s + cur, len - cur); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 772 | } |
| 773 | while (written < 0 && errno == EINTR); |
| 774 | |
| 775 | if (written < 0) |
| 776 | { |
| 777 | return AT_ERROR_GENERIC; |
| 778 | } |
| 779 | |
| 780 | cur += written; |
| 781 | } |
| 782 | |
| 783 | /* the \r */ |
| 784 | |
| 785 | do |
| 786 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 787 | written = write (s_at_fd[port], "\r", 1); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 788 | } |
| 789 | while ((written < 0 && errno == EINTR) || (written == 0)); |
| 790 | |
| 791 | if (written < 0) |
| 792 | { |
| 793 | return AT_ERROR_GENERIC; |
| 794 | } |
| 795 | |
| 796 | return 0; |
| 797 | } |
| 798 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 799 | static int writeCtrlZ (ATPortId_enum port, const char *s) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 800 | { |
| 801 | size_t cur = 0; |
| 802 | size_t len = strlen(s); |
| 803 | ssize_t written; |
| 804 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 805 | if (s_at_fd[port] < 0 || s_readerClosed > 0) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 806 | { |
| 807 | return AT_ERROR_CHANNEL_CLOSED; |
| 808 | } |
| 809 | |
| 810 | LOGD("AT> %s^Z\n", s); |
| 811 | |
| 812 | AT_DUMP( ">* ", s, strlen(s) ); |
| 813 | |
| 814 | /* the main string */ |
| 815 | while (cur < len) |
| 816 | { |
| 817 | do |
| 818 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 819 | written = write (s_at_fd[port], s + cur, len - cur); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 820 | } |
| 821 | while (written < 0 && errno == EINTR); |
| 822 | |
| 823 | if (written < 0) |
| 824 | { |
| 825 | return AT_ERROR_GENERIC; |
| 826 | } |
| 827 | |
| 828 | cur += written; |
| 829 | } |
| 830 | |
| 831 | /* the ^Z */ |
| 832 | |
| 833 | do |
| 834 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 835 | written = write (s_at_fd[port], "\032", 1); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 836 | } |
| 837 | while ((written < 0 && errno == EINTR) || (written == 0)); |
| 838 | |
| 839 | if (written < 0) |
| 840 | { |
| 841 | return AT_ERROR_GENERIC; |
| 842 | } |
| 843 | |
| 844 | return 0; |
| 845 | } |
| 846 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 847 | static void clearPendingCommand(ATPortId_enum port) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 848 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 849 | if (sp_response[port] != NULL) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 850 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 851 | at_response_free(sp_response[port]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 852 | } |
| 853 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 854 | sp_response[port] = NULL; |
| 855 | s_responsePrefix[port] = NULL; |
| 856 | s_smsPDU[port] = NULL; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | |
| 860 | /** |
| 861 | * Starts AT handler on stream "fd' |
| 862 | * returns 0 on success, -1 on error |
| 863 | */ |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 864 | int at_open(ATPortId_enum port, int at_fd, int uart_fd, ATUnsolHandler h) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 865 | { |
| 866 | int ret; |
| 867 | pthread_attr_t attr; |
| 868 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 869 | s_at_fd[port] = at_fd; |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 870 | s_uart_fd[port < ATPORTID_SIM2_0 ? MBTK_SIM_1 : MBTK_SIM_2] = uart_fd; |
| 871 | s_unsolHandler[port < ATPORTID_SIM2_0 ? MBTK_SIM_1 : MBTK_SIM_2] = h; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 872 | s_readerClosed = 0; |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 873 | s_responsePrefix[port] = NULL; |
| 874 | s_smsPDU[port] = NULL; |
| 875 | sp_response[port] = NULL; |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 876 | pthread_mutex_init(&s_commandmutex[port], NULL); |
| 877 | pthread_cond_init(&(s_commandcond[port]), NULL); |
| 878 | at_state_set(port,RIL_AT_STATE_READY); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 879 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 880 | ATPortId_enum *at_port_ptr = (ATPortId_enum*)malloc(sizeof(ATPortId_enum)); |
| 881 | ATPortId_enum *urc_port_ptr = (ATPortId_enum*)malloc(sizeof(ATPortId_enum)); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 882 | *at_port_ptr = port; |
| 883 | *urc_port_ptr = port; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 884 | |
| 885 | pthread_attr_init (&attr); |
| 886 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 887 | ret = pthread_create(&s_tid_reader[port], &attr, readerLoop, at_port_ptr); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 888 | if (ret < 0) |
| 889 | { |
| 890 | LOGE("AT thread create fail."); |
| 891 | return -1; |
| 892 | } |
| 893 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 894 | if(port == ATPORTID_SIM1_0 || port == ATPORTID_SIM2_0) { // URC only for ATPORTTYPE_0 |
b.liu | fd87baf | 2024-11-15 15:30:38 +0800 | [diff] [blame] | 895 | pthread_t uart_tid_reader; |
| 896 | ret = pthread_create(&uart_tid_reader, &attr, readerUrcLoop, urc_port_ptr); |
| 897 | if (ret < 0) |
| 898 | { |
| 899 | LOGE("Uart thread create fail."); |
| 900 | return -1; |
| 901 | } |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | return 0; |
| 905 | } |
| 906 | |
| 907 | /* FIXME is it ok to call this from the reader and the command thread? */ |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 908 | void at_close(ATPortId_enum port) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 909 | { |
| 910 | LOGD("at_close()"); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 911 | if (s_at_fd[port] >= 0) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 912 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 913 | close(s_at_fd[port]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 914 | } |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 915 | if (s_uart_fd[port < ATPORTID_SIM2_0 ? MBTK_SIM_1 : MBTK_SIM_2] >= 0) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 916 | { |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 917 | close(s_uart_fd[port < ATPORTID_SIM2_0 ? MBTK_SIM_1 : MBTK_SIM_2]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 918 | } |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 919 | s_at_fd[port] = -1; |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 920 | s_uart_fd[port < ATPORTID_SIM2_0 ? MBTK_SIM_1 : MBTK_SIM_2] = -1; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 921 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 922 | pthread_mutex_lock(&s_commandmutex[port]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 923 | s_readerClosed = 1; |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 924 | pthread_cond_signal(&s_commandcond[port]); |
| 925 | pthread_mutex_unlock(&s_commandmutex[port]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 926 | /* the reader thread should eventually die */ |
| 927 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 928 | at_state[port] = RIL_AT_STATE_CLOSED; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | static ATResponse * at_response_new() |
| 932 | { |
| 933 | return (ATResponse *) calloc(1, sizeof(ATResponse)); |
| 934 | } |
| 935 | |
| 936 | void at_response_free(ATResponse *p_response) |
| 937 | { |
| 938 | ATLine *p_line; |
| 939 | |
| 940 | if (p_response == NULL) return; |
| 941 | |
| 942 | p_line = p_response->p_intermediates; |
| 943 | |
| 944 | while (p_line != NULL) |
| 945 | { |
| 946 | ATLine *p_toFree; |
| 947 | |
| 948 | p_toFree = p_line; |
| 949 | p_line = p_line->p_next; |
| 950 | |
| 951 | free(p_toFree->line); |
| 952 | free(p_toFree); |
| 953 | } |
| 954 | |
| 955 | free (p_response->finalResponse); |
| 956 | free (p_response); |
| 957 | } |
| 958 | |
| 959 | /** |
| 960 | * The line reader places the intermediate responses in reverse order |
| 961 | * here we flip them back |
| 962 | */ |
| 963 | static void reverseIntermediates(ATResponse *p_response) |
| 964 | { |
| 965 | ATLine *pcur,*pnext; |
| 966 | |
| 967 | pcur = p_response->p_intermediates; |
| 968 | p_response->p_intermediates = NULL; |
| 969 | |
| 970 | while (pcur != NULL) |
| 971 | { |
| 972 | pnext = pcur->p_next; |
| 973 | pcur->p_next = p_response->p_intermediates; |
| 974 | p_response->p_intermediates = pcur; |
| 975 | pcur = pnext; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | static long long at_timeout_get(const char *at_command, bool *timeout_close) |
| 980 | { |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 981 | long long timeout = 15000; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 982 | int i; |
| 983 | for(i = 0; i < ARRAY_SIZE(at_timeout_list); i++) |
| 984 | { |
| 985 | if(!strncasecmp(at_command, at_timeout_list[i].at_command, strlen(at_timeout_list[i].at_command))) |
| 986 | { |
| 987 | timeout = at_timeout_list[i].timeout; |
| 988 | *timeout_close = at_timeout_list[i].timeout_close; |
| 989 | break; |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | return timeout; |
| 994 | } |
| 995 | |
| 996 | /** |
| 997 | * Internal send_command implementation |
| 998 | * Doesn't lock or call the timeout callback |
| 999 | * |
| 1000 | * timeoutMsec == 0 means infinite timeout |
| 1001 | */ |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 1002 | static int at_send_command_full_nolock (ATPortId_enum port, const char *command, ATCommandType type, |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1003 | const char *responsePrefix, const char *smspdu, |
| 1004 | long long timeoutMsec, ATResponse **pp_outResponse) |
| 1005 | { |
| 1006 | int err = 0; |
| 1007 | bool tiemout_close = true; |
| 1008 | struct timespec ts; |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1009 | |
| 1010 | LOGD("lynq 9, port %d %d",port,at_state[port]); |
| 1011 | if(sp_response[port] != NULL) |
| 1012 | { |
| 1013 | LOGE("lynq 10, port %d %p",port,sp_response[port]); |
| 1014 | err = AT_ERROR_COMMAND_PENDING; |
| 1015 | return err; |
| 1016 | } |
| 1017 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1018 | if(at_state[port] == RIL_AT_STATE_READY) |
| 1019 | at_state[port] = RIL_AT_STATE_BUSY; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1020 | |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1021 | LOGD("lynq 11, port %d",port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1022 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1023 | s_type[port] = type; |
| 1024 | s_responsePrefix[port] = responsePrefix; |
| 1025 | s_smsPDU[port] = smspdu; |
| 1026 | sp_response[port] = at_response_new(); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1027 | |
| 1028 | if(timeoutMsec == 0) |
| 1029 | { |
| 1030 | timeoutMsec = at_timeout_get(command, &tiemout_close); |
| 1031 | } |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1032 | err = writeline (port, command); |
| 1033 | |
| 1034 | if (err < 0) |
| 1035 | { |
| 1036 | goto error; |
| 1037 | } |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1038 | |
| 1039 | if (timeoutMsec != 0) |
| 1040 | { |
| 1041 | setTimespecRelative(&ts, timeoutMsec); |
| 1042 | } |
| 1043 | |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1044 | LOGD("lynq before loop, port %d %d",port,s_readerClosed); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1045 | while (sp_response[port]->finalResponse == NULL && s_readerClosed == 0) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1046 | { |
| 1047 | //LOGD("AT wait time:%lld",timeoutMsec); |
| 1048 | if (timeoutMsec != 0) |
| 1049 | { |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1050 | LOGD("lynq 12, port %d %lld",port,timeoutMsec); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1051 | err = pthread_cond_timedwait(&s_commandcond[port], &s_commandmutex[port], &ts); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1052 | } |
| 1053 | else |
| 1054 | { |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1055 | LOGD("lynq 13, port %d",port); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1056 | err = pthread_cond_wait(&s_commandcond[port], &s_commandmutex[port]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | //LOGD("AT continue:err - %d",err); |
| 1060 | if (err == ETIMEDOUT) |
| 1061 | { |
| 1062 | if(tiemout_close) |
| 1063 | { |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1064 | LOGE("lynq 15, port %d",port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1065 | err = AT_ERROR_TIMEOUT_CLOSE; |
| 1066 | } |
| 1067 | else |
| 1068 | { |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1069 | LOGE("lynq 16, port %d",port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1070 | err = AT_ERROR_TIMEOUT; |
| 1071 | } |
| 1072 | goto error; |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | if (pp_outResponse == NULL) |
| 1077 | { |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1078 | LOGD("lynq 18, port %d",port); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1079 | at_response_free(sp_response[port]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1080 | } |
| 1081 | else |
| 1082 | { |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1083 | LOGD("lynq 19, port %d",port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1084 | /* line reader stores intermediate responses in reverse order */ |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1085 | reverseIntermediates(sp_response[port]); |
| 1086 | *pp_outResponse = sp_response[port]; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1087 | } |
| 1088 | |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1089 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1090 | sp_response[port] = NULL; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1091 | |
| 1092 | if(s_readerClosed > 0) |
| 1093 | { |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1094 | |
| 1095 | LOGE("lynq 21, port %d %d",port,s_readerClosed); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1096 | err = AT_ERROR_CHANNEL_CLOSED; |
| 1097 | goto error; |
| 1098 | } |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1099 | LOGD("lynq 22, port %d",port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1100 | |
| 1101 | err = 0; |
| 1102 | error: |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1103 | if(at_state[port] == RIL_AT_STATE_BUSY) |
| 1104 | at_state[port] = RIL_AT_STATE_READY; |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1105 | LOGD("lynq 23, port %d %d",port, at_state[port]); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1106 | clearPendingCommand(port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1107 | return err; |
| 1108 | } |
| 1109 | |
| 1110 | /** |
| 1111 | * Internal send_command implementation |
| 1112 | * |
| 1113 | * timeoutMsec == 0 means infinite timeout |
| 1114 | */ |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 1115 | static int at_send_command_full (ATPortId_enum port, const char *command, ATCommandType type, |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1116 | const char *responsePrefix, const char *smspdu, |
| 1117 | long long timeoutMsec, ATResponse **pp_outResponse) |
| 1118 | { |
| 1119 | int err; |
| 1120 | |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1121 | |
| 1122 | LOGD("lynq 0, port %d,%lld,%s",port,timeoutMsec,command); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1123 | if (0 != pthread_equal(s_tid_reader[port], pthread_self())) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1124 | { |
| 1125 | /* cannot be called from reader thread */ |
| 1126 | LOGE("cannot be called from reader thread."); |
| 1127 | return AT_ERROR_INVALID_THREAD; |
| 1128 | } |
| 1129 | |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1130 | LOGD("lynq 1, port %d",port); |
| 1131 | |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1132 | // Waitting for previous AT complete. |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1133 | int count=0; |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1134 | while(at_state[port] == RIL_AT_STATE_BUSY) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1135 | { |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1136 | sleep(1); |
| 1137 | count++; |
| 1138 | if(count%2==0) |
| 1139 | { |
| 1140 | LOGE("lynq 2, port %d",port); |
| 1141 | } |
| 1142 | |
| 1143 | if(count==8) |
| 1144 | { |
| 1145 | err= AT_ERROR_COMMAND_PENDING; |
| 1146 | goto out; |
| 1147 | } |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1148 | } |
| 1149 | |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1150 | LOGD("lynq 3, port %d",port); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1151 | pthread_mutex_lock(&s_commandmutex[port]); |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1152 | LOGD("lynq 4, port %d",port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1153 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1154 | err = at_send_command_full_nolock(port, command, type, |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1155 | responsePrefix, smspdu, |
| 1156 | timeoutMsec, pp_outResponse); |
| 1157 | |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1158 | LOGD("lynq 5, port %d",port); |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1159 | pthread_mutex_unlock(&s_commandmutex[port]); |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1160 | LOGD("lynq 6, port %d",port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1161 | |
| 1162 | if (err == AT_ERROR_TIMEOUT_CLOSE && s_onTimeout != NULL) |
| 1163 | { |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1164 | LOGE("lynq 7, port %d",port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1165 | s_onTimeout(); |
| 1166 | } |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1167 | LOGD("lynq 8, port %d",port); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1168 | |
q.huang | ed00422 | 2025-09-26 21:33:56 +0800 | [diff] [blame^] | 1169 | out: |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1170 | return err; |
| 1171 | } |
| 1172 | |
| 1173 | |
| 1174 | /** |
| 1175 | * Issue a single normal AT command with no intermediate response expected |
| 1176 | * |
| 1177 | * "command" should not include \r |
| 1178 | * pp_outResponse can be NULL |
| 1179 | * |
| 1180 | * if non-NULL, the resulting ATResponse * must be eventually freed with |
| 1181 | * at_response_free |
| 1182 | */ |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 1183 | int at_send_command (ATPortId_enum port, const char *command, ATResponse **pp_outResponse) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1184 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1185 | return at_send_command_full (port, command, NO_RESULT, NULL, |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1186 | NULL, 0, pp_outResponse); |
| 1187 | } |
| 1188 | |
| 1189 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 1190 | int at_send_command_singleline (ATPortId_enum port, const char *command, |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1191 | const char *responsePrefix, |
| 1192 | ATResponse **pp_outResponse) |
| 1193 | { |
| 1194 | int err; |
| 1195 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1196 | err = at_send_command_full (port, command, SINGLELINE, responsePrefix, |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1197 | NULL, 0, pp_outResponse); |
| 1198 | |
| 1199 | if (err == 0 && pp_outResponse != NULL |
| 1200 | && (*pp_outResponse)->success > 0 |
| 1201 | && (*pp_outResponse)->p_intermediates == NULL |
| 1202 | ) |
| 1203 | { |
| 1204 | /* successful command must have an intermediate response */ |
| 1205 | at_response_free(*pp_outResponse); |
| 1206 | *pp_outResponse = NULL; |
| 1207 | return AT_ERROR_INVALID_RESPONSE; |
| 1208 | } |
| 1209 | |
| 1210 | return err; |
| 1211 | } |
| 1212 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 1213 | int at_send_command_singleline_with_timeout (ATPortId_enum port, const char *command, |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1214 | const char *responsePrefix, |
| 1215 | ATResponse **pp_outResponse,long long timeoutMsec) |
| 1216 | { |
| 1217 | int err; |
| 1218 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1219 | err = at_send_command_full (port, command, SINGLELINE, responsePrefix, |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1220 | NULL, timeoutMsec, pp_outResponse); |
| 1221 | |
| 1222 | if (err == 0 && pp_outResponse != NULL |
| 1223 | && (*pp_outResponse)->success > 0 |
| 1224 | && (*pp_outResponse)->p_intermediates == NULL |
| 1225 | ) |
| 1226 | { |
| 1227 | /* successful command must have an intermediate response */ |
| 1228 | at_response_free(*pp_outResponse); |
| 1229 | *pp_outResponse = NULL; |
| 1230 | return AT_ERROR_INVALID_RESPONSE; |
| 1231 | } |
| 1232 | |
| 1233 | return err; |
| 1234 | } |
| 1235 | |
| 1236 | |
| 1237 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 1238 | int at_send_command_numeric (ATPortId_enum port, const char *command, |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1239 | ATResponse **pp_outResponse) |
| 1240 | { |
| 1241 | int err; |
| 1242 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1243 | err = at_send_command_full (port, command, NUMERIC, NULL, |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1244 | NULL, 0, pp_outResponse); |
| 1245 | |
| 1246 | if (err == 0 && pp_outResponse != NULL |
| 1247 | && (*pp_outResponse)->success > 0 |
| 1248 | && (*pp_outResponse)->p_intermediates == NULL |
| 1249 | ) |
| 1250 | { |
| 1251 | /* successful command must have an intermediate response */ |
| 1252 | at_response_free(*pp_outResponse); |
| 1253 | *pp_outResponse = NULL; |
| 1254 | return AT_ERROR_INVALID_RESPONSE; |
| 1255 | } |
| 1256 | |
| 1257 | return err; |
| 1258 | } |
| 1259 | |
| 1260 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 1261 | int at_send_command_sms (ATPortId_enum port, const char *command, |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1262 | const char *pdu, |
| 1263 | const char *responsePrefix, |
| 1264 | ATResponse **pp_outResponse) |
| 1265 | { |
| 1266 | int err; |
| 1267 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1268 | err = at_send_command_full (port, command, SINGLELINE, responsePrefix, |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1269 | pdu, 0, pp_outResponse); |
| 1270 | |
| 1271 | if (err == 0 && pp_outResponse != NULL |
| 1272 | && (*pp_outResponse)->success > 0 |
| 1273 | && (*pp_outResponse)->p_intermediates == NULL |
| 1274 | ) |
| 1275 | { |
| 1276 | /* successful command must have an intermediate response */ |
| 1277 | at_response_free(*pp_outResponse); |
| 1278 | *pp_outResponse = NULL; |
| 1279 | return AT_ERROR_INVALID_RESPONSE; |
| 1280 | } |
| 1281 | |
| 1282 | return err; |
| 1283 | } |
| 1284 | |
| 1285 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 1286 | int at_send_command_multiline (ATPortId_enum port, const char *command, |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1287 | const char *responsePrefix, |
| 1288 | ATResponse **pp_outResponse) |
| 1289 | { |
| 1290 | int err; |
| 1291 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1292 | err = at_send_command_full (port, command, MULTILINE, responsePrefix, |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1293 | NULL, 0, pp_outResponse); |
| 1294 | |
| 1295 | return err; |
| 1296 | } |
| 1297 | |
| 1298 | |
| 1299 | /** This callback is invoked on the command thread */ |
| 1300 | void at_set_on_timeout(void (*onTimeout)(void)) |
| 1301 | { |
| 1302 | s_onTimeout = onTimeout; |
| 1303 | } |
| 1304 | |
| 1305 | /** |
| 1306 | * This callback is invoked on the reader thread (like ATUnsolHandler) |
| 1307 | * when the input stream closes before you call at_close |
| 1308 | * (not when you call at_close()) |
| 1309 | * You should still call at_close() |
| 1310 | */ |
| 1311 | |
| 1312 | void at_set_on_reader_closed(void (*onClose)(void)) |
| 1313 | { |
| 1314 | s_onReaderClosed = onClose; |
| 1315 | } |
| 1316 | |
| 1317 | |
| 1318 | /** |
| 1319 | * Periodically issue an AT command and wait for a response. |
| 1320 | * Used to ensure channel has start up and is active |
| 1321 | */ |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 1322 | int at_handshake(ATPortId_enum port) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1323 | { |
b.liu | 62240ee | 2024-11-07 17:52:45 +0800 | [diff] [blame] | 1324 | // int i; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1325 | int err = 0; |
| 1326 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1327 | if (0 != pthread_equal(s_tid_reader[port], pthread_self())) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1328 | { |
| 1329 | /* cannot be called from reader thread */ |
| 1330 | return AT_ERROR_INVALID_THREAD; |
| 1331 | } |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1332 | pthread_mutex_lock(&s_commandmutex[port]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1333 | |
| 1334 | #if 0 |
| 1335 | for (i = 0 ; i < HANDSHAKE_RETRY_COUNT ; i++) |
| 1336 | { |
| 1337 | /* some stacks start with verbose off */ |
| 1338 | err = at_send_command_full_nolock("ATE0Q0V1", NO_RESULT, |
| 1339 | NULL, NULL, HANDSHAKE_TIMEOUT_MSEC, NULL); |
| 1340 | |
| 1341 | if (err == 0) |
| 1342 | { |
| 1343 | break; |
| 1344 | } |
| 1345 | } |
| 1346 | #else |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1347 | err = at_send_command_full_nolock(port, "ATE0Q0V1", NO_RESULT, |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1348 | NULL, NULL, 0, NULL); |
| 1349 | #endif |
| 1350 | |
| 1351 | if (err == 0) |
| 1352 | { |
| 1353 | /* pause for a bit to let the input buffer drain any unmatched OK's |
| 1354 | (they will appear as extraneous unsolicited responses) */ |
| 1355 | sleepMsec(HANDSHAKE_TIMEOUT_MSEC); |
| 1356 | } |
| 1357 | |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1358 | pthread_mutex_unlock(&s_commandmutex[port]); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1359 | |
| 1360 | |
| 1361 | return err; |
| 1362 | } |
| 1363 | |
| 1364 | /** |
| 1365 | * Returns error code from response |
| 1366 | * Assumes AT+CMEE=1 (numeric) mode |
| 1367 | */ |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame] | 1368 | AT_CME_Error at_get_cme_error(const ATResponse *p_response) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1369 | { |
| 1370 | int ret; |
| 1371 | int err; |
| 1372 | char *p_cur; |
| 1373 | |
b.liu | b618090 | 2025-07-02 18:48:23 +0800 | [diff] [blame] | 1374 | if (p_response == NULL) |
| 1375 | { |
| 1376 | return CME_ERROR_NON_CME; |
| 1377 | } |
| 1378 | |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1379 | if (p_response->success > 0) |
| 1380 | { |
| 1381 | return CME_SUCCESS; |
| 1382 | } |
| 1383 | |
| 1384 | if (p_response->finalResponse == NULL |
| 1385 | || !strStartsWith(p_response->finalResponse, "+CME ERROR:") |
| 1386 | ) |
| 1387 | { |
| 1388 | return CME_ERROR_NON_CME; |
| 1389 | } |
| 1390 | |
| 1391 | p_cur = p_response->finalResponse; |
| 1392 | err = at_tok_start(&p_cur); |
| 1393 | |
| 1394 | if (err < 0) |
| 1395 | { |
| 1396 | return CME_ERROR_NON_CME; |
| 1397 | } |
| 1398 | |
| 1399 | err = at_tok_nextint(&p_cur, &ret); |
| 1400 | |
| 1401 | if (err < 0) |
| 1402 | { |
| 1403 | return CME_ERROR_NON_CME; |
| 1404 | } |
| 1405 | |
| 1406 | return ret; |
| 1407 | } |
| 1408 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 1409 | mbtk_ril_at_state_enum at_state_get(ATPortId_enum port) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1410 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1411 | return at_state[port]; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1412 | } |
| 1413 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 1414 | void at_state_set(ATPortId_enum port, mbtk_ril_at_state_enum state) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1415 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1416 | at_state[port] = state; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | bool at_rsp_check(ATResponse *p_response) |
| 1420 | { |
| 1421 | if(!p_response || !p_response->success) |
| 1422 | return false; |
| 1423 | |
| 1424 | return true; |
| 1425 | } |
| 1426 | |
b.liu | 7ca612c | 2025-04-25 09:23:36 +0800 | [diff] [blame] | 1427 | void unused_func(ATPortId_enum port) |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1428 | { |
b.liu | b171c9a | 2024-11-12 19:23:29 +0800 | [diff] [blame] | 1429 | isFinalResponse(port, NULL); |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1430 | } |
| 1431 | |