3232 init /1 ,
3333 handle_call /3 ,
3434 handle_cast /2 ,
35+ handle_continue /2 ,
3536 handle_info /2 ,
3637 terminate /2
3738]).
@@ -270,31 +271,11 @@ wait_for_ap(ApConfig, Timeout) ->
270271% %-----------------------------------------------------------------------------
271272-spec start (Config :: network_config ()) -> {ok , pid ()} | {error , Reason :: term ()}.
272273start (Config ) ->
273- case gen_server :start ({local , ? MODULE }, ? MODULE , Config , []) of
274- {ok , Pid } = R ->
275- case gen_server :call (Pid , start ) of
276- ok ->
277- R ;
278- Error ->
279- Error
280- end ;
281- Error ->
282- Error
283- end .
274+ gen_server :start ({local , ? MODULE }, ? MODULE , Config , []).
284275
285276-spec start_link (Config :: network_config ()) -> {ok , pid ()} | {error , Reason :: term ()}.
286277start_link (Config ) ->
287- case gen_server :start_link ({local , ? MODULE }, ? MODULE , Config , []) of
288- {ok , Pid } = R ->
289- case gen_server :call (Pid , start ) of
290- ok ->
291- R ;
292- Error ->
293- Error
294- end ;
295- Error ->
296- Error
297- end .
278+ gen_server :start_link ({local , ? MODULE }, ? MODULE , Config , []).
298279
299280% %-----------------------------------------------------------------------------
300281% % @returns ok, if the network interface was stopped, or {error, Reason} if
@@ -314,13 +295,17 @@ stop() ->
314295% %-----------------------------------------------------------------------------
315296-spec sta_rssi () -> {ok , Rssi :: db ()} | {error , Reason :: term ()}.
316297sta_rssi () ->
317- Port = get_port (),
318- Ref = make_ref (),
319- Port ! {self (), Ref , rssi },
320- receive
321- {Ref , {error , Reason }} -> {error , Reason };
322- {Ref , {rssi , Rssi }} -> {ok , Rssi };
323- Other -> {error , Other }
298+ case whereis (network_port ) of
299+ undefined ->
300+ {error , network_down };
301+ Port ->
302+ Ref = make_ref (),
303+ Port ! {self (), Ref , rssi },
304+ receive
305+ {Ref , {error , Reason }} -> {error , Reason };
306+ {Ref , {rssi , Rssi }} -> {ok , Rssi };
307+ Other -> {error , Other }
308+ end
324309 end .
325310
326311% %
@@ -329,28 +314,23 @@ sta_rssi() ->
329314
330315% % @hidden
331316init (Config ) ->
332- {ok , # state {config = Config }}.
333-
334- % % @hidden
335- handle_call (start , From , # state {config = Config } = State ) ->
336317 Port = get_port (),
337318 Ref = make_ref (),
338- Port ! {self (), Ref , {start , Config }},
339- wait_start_reply (Ref , From , Port , State );
340- handle_call (_Msg , _From , State ) ->
341- {reply , {error , unknown_message }, State }.
319+ {ok , # state {config = Config , port = Port , ref = Ref }, {continue , start_port }}.
342320
343- % % @private
344- wait_start_reply ( Ref , From , Port , State ) ->
321+ handle_continue ( start_port , # state { config = Config , port = Port , ref = Ref } = State ) ->
322+ Port ! { self (), Ref , { start , Config }},
345323 receive
346324 {Ref , ok } ->
347- gen_server :reply (From , ok ),
348- {noreply , State # state {port = Port , ref = Ref }};
349- {Ref , {error , Reason } = ER } ->
350- gen_server :reply (From , {error , Reason }),
351- {stop , {start_failed , Reason }, ER , State }
325+ {noreply , State };
326+ {Ref , {error , Reason }} ->
327+ {stop , {start_port_failed , Reason }, State }
352328 end .
353329
330+ % % @hidden
331+ handle_call (_Msg , _From , State ) ->
332+ {reply , {error , unknown_message }, State }.
333+
354334% % @hidden
355335handle_cast (_Msg , State ) ->
356336 {noreply , State }.
@@ -390,10 +370,25 @@ handle_info(Msg, State) ->
390370 {noreply , State }.
391371
392372% % @hidden
393- terminate (_Reason , _State ) ->
373+ % % Wait for port to be closed
374+ terminate (_Reason , State ) ->
394375 Ref = make_ref (),
376+ Port = State # state .port ,
377+ PortMonitor = erlang :monitor (port , Port ),
395378 network_port ! {? SERVER , Ref , stop },
396- ok .
379+ wait_for_port_close (PortMonitor , Port ).
380+
381+ wait_for_port_close (PortMonitor , Port ) ->
382+ receive
383+ {'DOWN' , PortMonitor , port , Port , _DownReason } ->
384+ ok ;
385+ _Other ->
386+ % Handle unexpected messages if necessary
387+ wait_for_port_close (PortMonitor , Port )
388+ % Timeout after 1 second just in case.
389+ after 1000 ->
390+ {error , timeout }
391+ end .
397392
398393% %
399394% % Internal operations
0 commit comments