* Send commands to AT server and wait response. * * @param client current AT client object * @param resp AT response object, using RT_NULL when you don't care response * @param cmd_expr AT commands expression * * @return 0 : success * -1 : response status error * -2 : wait timeout * -7 : enter AT CLI mode */
| 295 | * -7 : enter AT CLI mode |
| 296 | */ |
| 297 | int at_obj_exec_cmd(at_client_t client, at_response_t resp, const char *cmd_expr, ...) |
| 298 | { |
| 299 | va_list args; |
| 300 | rt_err_t result = RT_EOK; |
| 301 | |
| 302 | RT_ASSERT(cmd_expr); |
| 303 | |
| 304 | if (client == RT_NULL) |
| 305 | { |
| 306 | LOG_E("input AT Client object is NULL, please create or get AT Client object!"); |
| 307 | return -RT_ERROR; |
| 308 | } |
| 309 | |
| 310 | /* check AT CLI mode */ |
| 311 | if (client->status == AT_STATUS_CLI && resp) |
| 312 | { |
| 313 | return -RT_EBUSY; |
| 314 | } |
| 315 | |
| 316 | rt_mutex_take(&client->lock, RT_WAITING_FOREVER); |
| 317 | |
| 318 | client->resp_status = AT_RESP_OK; |
| 319 | |
| 320 | if (resp != RT_NULL) |
| 321 | { |
| 322 | resp->buf_len = 0; |
| 323 | resp->line_counts = 0; |
| 324 | } |
| 325 | |
| 326 | client->resp = resp; |
| 327 | rt_event_recv(&client->event, at_client_resp_notice_event, RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, 0, NULL); |
| 328 | |
| 329 | va_start(args, cmd_expr); |
| 330 | client->last_cmd_len = at_vprintfln(client->device, client->send_buf, client->send_bufsz, cmd_expr, args); |
| 331 | if (client->last_cmd_len > 2) |
| 332 | { |
| 333 | client->last_cmd_len -= 2; /* "\r\n" */ |
| 334 | } |
| 335 | va_end(args); |
| 336 | |
| 337 | if (resp != RT_NULL) |
| 338 | { |
| 339 | if (rt_event_recv(&client->event, at_client_resp_notice_event, RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, resp->timeout, NULL) != RT_EOK) |
| 340 | { |
| 341 | LOG_W("execute command (%.*s) timeout (%d ticks)!", client->last_cmd_len, client->send_buf, resp->timeout); |
| 342 | client->resp_status = AT_RESP_TIMEOUT; |
| 343 | result = -RT_ETIMEOUT; |
| 344 | } |
| 345 | else if (client->resp_status != AT_RESP_OK) |
| 346 | { |
| 347 | LOG_E("execute command (%.*s) failed!", client->last_cmd_len, client->send_buf); |
| 348 | result = -RT_ERROR; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | client->resp = RT_NULL; |
| 353 | |
| 354 | rt_mutex_release(&client->lock); |
no test coverage detected