anchor.diff:      end.
   
   %%%===================================================================
  -%%% gen_fsm callbacks
  +%%% gen_statem callbacks
   %%%===================================================================
  -
  -init([SCG = {SCGIP, SCGControlPort}, IP, Port, CertDir, RootCert, Mac, RemoteMode, Owner, Options]) ->
  -    {ok, ControlSocket} = capwap_udp:connect(SCGIP, SCGControlPort, [{active, false}, {mode, binary}, {ip, IP}]),
  -
  -    DataSocket = case RemoteMode of
  -		     true ->
  -			 {ok, UdpDataSocket} = capwap_udp:connect(SCGIP, SCGControlPort + 1, [{active, false}, {mode, binary}, {ip, IP}]),
  -			 ok = capwap_udp:setopts(UdpDataSocket, [{active, true}]),
  -			 UdpDataSocket;
  -		     false ->
  -			 undefined
  -		 end,
  -
  -    {ok, idle, #state{control_socket = ControlSocket,
  -		      data_socket = DataSocket,
  -		      ctrl_stream = capwap_stream:init(1500),
  -		      owner = Owner,
  -		      seqno = 0,
  -		      stations = [],
  -		      remote_mode = RemoteMode,
  -		      cert_dir = CertDir,
  -		      root_cert = RootCert,
  -		      ip = IP,
  -		      mac = Mac,
  -		      scg = SCG,
  -		      simulated_data_port = Port,
  -		      next_resp = undefined,
  -		      echo_request_timeout = 0,
  -		      keep_alive_timeout = 0,
  -		      capwap_wtp_session_id = random:uniform(329785637896618622174542098706248598340),
  -		      wifi_up = false,
  -		      request_pending = undefined,
  -              options = Options
  -		     }}.
  -
  -%%--------------------------------------------------------------------
  -%% @private
  -%% @doc
  -%% There should be one instance of this function for each possible
  -%% state name. Whenever a gen_fsm receives an event sent using
  -%% gen_fsm:send_event/2, the instance of this function with the same
  -%% name as the current state name StateName is called to handle
  -%% the event. It is also called if a timeout occurs.
  -%%
  -%% @spec state_name(Event, State) ->
  -%%                   {next_state, NextStateName, NextState} |
  -%%                   {next_state, NextStateName, NextState, Timeout} |
  -%%                   {stop, Reason, NewState}
  -%% @end
  -%%--------------------------------------------------------------------
  -idle(_Event, State) ->
  -    {next_state, idle, State}.
  -
  -idle(send_discovery, _From, State) ->
  -    IEs = [#discovery_type{discovery_type = static}
  -	  ] ++ create_default_ies(),
  -    {resp, Resp, State0} = do_transition(State, control, discovery, {discovery_request, IEs}, udp_sync, req, undefined),
  +callback_mode() ->
  +    handle_event_function.
  +
  +init([SCG = {SCGIP, SCGControlPort}, IP, Port,
  +      CertDir, RootCert, Mac, RemoteMode, Owner, Options]) ->
  +    {ok, ControlSocket} =
  +	capwap_udp:connect(SCGIP, SCGControlPort, [{active, false}, {mode, binary}, {ip, IP}]),
  +
  +    DataSocket =
  +	case RemoteMode of
  +	    true ->
  +		{ok, UdpDataSocket} =
  +		    capwap_udp:connect(SCGIP, SCGControlPort + 1,
  +				       [{active, false}, {mode, binary}, {ip, IP}]),
  +		ok = capwap_udp:setopts(UdpDataSocket, [{active, true}]),
  +		UdpDataSocket;
  +	    false ->
  +		undefined
  +	end,
  +
  +    Data = #data{
  +	      control_socket = ControlSocket,
  +	      data_socket = DataSocket,
  +	      ctrl_stream = capwap_stream:init(1500),
  +	      owner = Owner,
  +	      seqno = 0,
  +	      stations = [],
  +	      remote_mode = RemoteMode,
  +	      cert_dir = CertDir,
  +	      root_cert = RootCert,
  +	      ip = IP,
  +	      mac = Mac,
  +	      scg = SCG,
  +	      simulated_data_port = Port,
  +	      next_resp = undefined,
  +	      echo_request_timeout = 0,
  +	      keep_alive_timeout = 0,
  +	      capwap_wtp_session_id = rand:uniform(329785637896618622174542098706248598340),
  +	      wifi_up = false,
  +	      request_pending = undefined,
  +	      options = Options
  +	     },
  +    {ok, idle, Data}.
  +
  +handle_event({call, From}, send_discovery, idle, Data) ->
  +    IEs = [#discovery_type{discovery_type = static}]
  +	++ create_default_ies(),
  +    {resp, Resp, Data0, Actions} =
  +	do_transition(Data, control, {discovery_request, IEs},
  +		      udp_sync, req, undefined),
       lager:debug("got discovery response:  ~p", [Resp]),
  -    {reply, {ok, Resp}, discovery, State0};
  -
  -idle(_Event, _From, State) ->
  -    {reply, {error, bad_event}, idle, State}.
  -
  -discovery(_Event, State) ->
  -    {next_state, discovery, State}.
  -
  -discovery(send_join, From, State=#state{control_socket = CS, ip = IP,
  -					 capwap_wtp_session_id = CapwapWtpSessionId}) ->
  -    S1 = case State#state.remote_mode of
  +    {next_state, discovery, Data0, [{reply, From, {ok, Resp}} | Actions]};
  +
  +handle_event({call, From}, send_join, discovery,
  +	     Data = #data{control_socket = CS, ip = IP,
  +			  capwap_wtp_session_id = CapwapWtpSessionId}) ->
  +    S1 = case Data#data.remote_mode of
   	     true ->
  -		 lager:debug("connecting ssl socket with options ~p", [make_ssl_options(State)]),
  +		 lager:debug("connecting ssl socket with options ~p", [make_ssl_options(Data)]),
   		 ok = capwap_udp:setopts(CS, [{active, true}]),
  -		 {ok, SSLSocket} = ssl:connect(CS, make_ssl_options(State)),
  +		 {ok, SSLSocket} = ssl:connect(CS, make_ssl_options(Data)),
   		 ok = ssl:setopts(SSLSocket, [{active, true}]),
   		 lager:debug("successfully connected ssl socket", []),
  -		 State#state{control_socket = SSLSocket};
  +		 Data#data{control_socket = SSLSocket};
   	     _ ->
   		 ok = capwap_udp:setopts(CS, [{active, true}]),
  -		 State
  +		 Data
   	 end,
   
       IEs = [#location_data{location = <<"  Next to Fridge">>},
   	   #local_ipv4_address{ip_address = tuple_to_ip(IP)},
   	   #wtp_name{wtp_name = <<"My WTP 1">>},
  -	   #session_id{session_id = CapwapWtpSessionId}
  -	  ] ++ create_default_ies(),
  -    do_transition(S1, control, discovery, {join_request, IEs}, async, req, {join_response, From});
  -
  -discovery(_Event, _From, State) ->
  -    {reply, {error, bad_event}, discovery, State}.
  -
  -join(_Event, State) ->
  -    {next_state, join, State}.
  -
  -join(send_config_status, From, State) ->
  +	   #session_id{session_id = CapwapWtpSessionId}]
  +	++ create_default_ies(),
  +    {data, DataNew, Actions} =
  +	do_transition(S1, control, {join_request, IEs},
  +		      async, req, {join_response, From}),
  +    {keep_state, DataNew, Actions};
  +
  +handle_event({call, From}, send_config_status, join, Data) ->
       IEs = [#ac_name{name = <<" My AC">>},
   	   #ac_name_with_priority{priority = 0, name = <<"ACPrimary">>},
   	   #ac_name_with_priority{priority = 1, name = <<"ACSecondary">>},
  
positive.code:     DA = <<1:48>>,
      SA = Mac,
      BSS = WTPMac,
      SequenceControl = get_seqno(State),
      Frame = <<0:8>>,
      Payload = <<FrameControl:2/bytes,
  		Duration:16, DA:6/bytes, SA:6/bytes, BSS:6/bytes,
  		SequenceControl:16/little-integer, Frame/binary>>,
      Flags=[{frame, native}],
      lager:info("in state run adding station: ~p", [Mac]),
      do_transition(State, data, run, {Flags, Payload}, async, req, {add_station_resp, From});
  
      %% this transition provokes an error which occured before request queue was introduce into capwap_ac
      %% {TypeDis, SubTypeDis} = ieee80211_station:frame_type('Disassociation'),
  
negative.code: 	 send_keep_alive/1
  	]).
  
  %% gen_statem callbacks
  -export([callback_mode/0, init/1, handle_event/4, terminate/3, code_change/4]).
  
  -define(SERVER, ?MODULE).
  -define(Default_WTP_MAC, <<8,8,8,8,8,8>>).
  -define(Default_Local_Control_Port, 5248).
  -define(Default_SCG, {{127,0,0,1}, 5246}).
  
  -record(data, {control_socket,
  	       data_socket,
  	       ctrl_stream,
  	       owner,
  	       seqno,
  	       stations,
  	       remote_mode,
  	       cert_dir,
  	       root_cert,
  	       ip,
  	       mac,
  	       scg,
  	       simulated_data_port,
  	       next_resp,
  	       echo_request_timeout,
  	       capwap_wtp_session_id,
  	       wifi_up,
  	       request_pending,
  	       keep_alive_timeout,
  	       options
  	      }).
  
  %%%===================================================================
  %%% API
  