@@ -329,6 +329,63 @@ int at_exec_cmd(at_response_t resp, const char *cmd_expr, ...)
329329 return result ;
330330}
331331
332+ /**
333+ * Waiting for connection to external devices.
334+ *
335+ * @param timeout millisecond for timeout
336+ *
337+ * @return 0 : success
338+ * -2 : timeout
339+ * -5 : no memory
340+ */
341+ int at_client_wait_connect (rt_uint32_t timeout )
342+ {
343+ rt_err_t result = RT_EOK ;
344+ at_response_t resp = RT_NULL ;
345+ at_client_t client = at_client_local ;
346+ rt_tick_t start_time = 0 ;
347+
348+ resp = at_create_resp (16 , 0 , rt_tick_from_millisecond (500 ));
349+ if (!resp )
350+ {
351+ LOG_E ("No memory for response structure!" );
352+ return - RT_ENOMEM ;
353+ }
354+
355+ rt_mutex_take (client -> lock , RT_WAITING_FOREVER );
356+ client -> resp = resp ;
357+
358+ start_time = rt_tick_get ();
359+
360+ while (1 )
361+ {
362+ /* Check whether it is timeout */
363+ if (rt_tick_get () - start_time > timeout )
364+ {
365+ LOG_E ("wait connect timeout (%d millisecond)!" , timeout );
366+ result = - RT_ETIMEOUT ;
367+ break ;
368+ }
369+
370+ /* Check whether it is already connected */
371+ resp -> line_counts = 0 ;
372+ rt_device_write (client -> device , 0 , "AT\r\n" , 4 );
373+
374+ if (rt_sem_take (client -> resp_notice , resp -> timeout ) != RT_EOK )
375+ continue ;
376+ else
377+ break ;
378+ }
379+
380+ at_delete_resp (resp );
381+
382+ client -> resp = RT_NULL ;
383+
384+ rt_mutex_release (client -> lock );
385+
386+ return result ;
387+ }
388+
332389/**
333390 * Send data to AT server, send data don't have end sign(eg: \r\n).
334391 *
0 commit comments