summaryrefslogtreecommitdiff
path: root/components/dlink_bt/src/bt_connection.erl
blob: ab6d5da370ea1cb7e6395abdc01294c8e79df0df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
%%
%% Copyright (C) 2014, Jaguar Land Rover
%%
%% This program is licensed under the terms and conditions of the
%% Mozilla Public License, version 2.0.  The full text of the 
%% Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
%%

%%%-------------------------------------------------------------------
%%% @author magnus <magnus@t520.home>
%%% @copyright (C) 2014, magnus
%%% @doc
%%%
%%% @end
%%% Created : 12 Sep 2014 by magnus <magnus@t520.home>
%%%-------------------------------------------------------------------
-module(bt_connection).

-behaviour(gen_server).
-include_lib("lager/include/log.hrl").

%% API

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
	 terminate/2, code_change/3]).

-export([connect/5]).
-export([accept/5]).
-export([send/2]).
-export([send/3]).
-export([is_connection_up/1]).
-export([is_connection_up/2]).
-export([terminate_connection/1]).
-export([terminate_connection/2]).


-define(SERVER, ?MODULE). 

-record(st, {
	  remote_addr = "00:00:00:00:00:00",
	  channel = 0,
	  rfcomm_ref = undefined,
	  mod = undefined,
	  func = undefined,
	  args = undefined
	 }).

%%%===================================================================
%%% API
%%%===================================================================
%% MFA is to deliver data received on the socket.

connect(Addr, Channel, Mod, Fun, Arg) ->
    gen_server:start_link(?MODULE, 
			  {connect, Addr, Channel, Mod, Fun, Arg },
			  []).

accept(Channel, ListenRef, Mod, Fun, Arg) ->
    gen_server:start_link(?MODULE, {accept, 
				    Channel, 
				    ListenRef, 
				    Mod,
				    Fun, 
				    Arg},[]).

send(Pid, Data) when is_pid(Pid) ->
    gen_server:cast(Pid, {send, Data}).
    
send(Addr, Channel, Data) ->
    case bt_connection_manager:find_connection_by_address(Addr, Channel) of
	{ok, Pid} ->
	    gen_server:cast(Pid, {send, Data});

	_Err -> 
	    ?info("connection:send(): Connection ~p:~p not found for data: ~p", 
		  [ Addr, Channel, Data]),
	    not_found

    end.

terminate_connection(Pid) when is_pid(Pid) ->
    gen_server:call(Pid, terminate_connection).
    
terminate_connection(Addr, Channel) ->
    case bt_connection_manager:find_connection_by_address(Addr, Channel) of
	{ok, Pid} ->
	    gen_server:call(Pid, terminate_connection);

	_Err -> not_found
    end.


is_connection_up(Pid) when is_pid(Pid) ->
    is_process_alive(Pid).

is_connection_up(Addr, Channel) ->
    case bt_connection_manager:find_connection_by_address(Addr, Channel) of
	{ok, Pid} ->
	    is_connection_up(Pid);

	_Err -> 
	    false
    end.
    
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Initializes the server
%%
%% @spec init(Args) -> {ok, State} |
%%                     {ok, State, Timeout} |
%%                     ignore |
%%                     {stop, Reason}
%% @end
%%--------------------------------------------------------------------
%% MFA used to handle socket closed, socket error and received data
%% When data is received, a separate process is spawned to handle
%% the MFA invocation.
init({connect, BTAddr, Channel, Mod, Fun, Arg}) ->
    
    %% connect will block on rfcomm:open, so cast to self
    %% in order to let init return.
    gen_server:cast(self(), connect),
    {ok, #st{
	    remote_addr = BTAddr,
	    channel = Channel,
	    rfcomm_ref = undefined,
	    mod = Mod,
	    func = Fun,
	    args = Arg
	   }};



init({accept, Channel, ListenRef, Mod, Fun, Arg}) ->
    { ok, ARef } = rfcomm:accept(ListenRef, infinity, self()),
    ?debug("bt_connection:init(accept): self():    ~p", [self()]),
    ?debug("bt_connection:init(accept): Channel:   ~p", [Channel]),
    ?debug("bt_connection:init(accept): ListenRef: ~p", [ListenRef]),
    ?debug("bt_connection:init(accept): AcceptRef: ~p", [ARef]),
    ?debug("bt_connection:init(accept): Module:    ~p", [Mod]),
    ?debug("bt_connection:init(accept): Function:  ~p", [Fun]),
    ?debug("bt_connection:init(accept): Arg:       ~p", [Arg]),

    {ok, #st{
	    channel = Channel,
	    rfcomm_ref = ARef,
	    mod = Mod,
	    func = Fun,
	    args = Arg
	   }}.


%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling call messages
%%
%% @spec handle_call(Request, From, State) ->
%%                                   {reply, Reply, State} |
%%                                   {reply, Reply, State, Timeout} |
%%                                   {noreply, State} |
%%                                   {noreply, State, Timeout} |
%%                                   {stop, Reason, Reply, State} |
%%                                   {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_call(terminate_connection, _From,  St) ->
    ?debug("~p:handle_call(terminate_connection): Terminating: ~p", 
	     [ ?MODULE, {St#st.remote_addr, St#st.channel}]),

    {stop, Reason, NSt} = handle_info({tcp_closed, St#st.rfcomm_ref}, St),
    {stop, Reason, ok, NSt};

handle_call(_Request, _From, State) ->
    ?warning("~p:handle_call(): Unknown call: ~p", [ ?MODULE, _Request]),
    Reply = ok,
    {reply, Reply, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling cast messages
%%
%% @spec handle_cast(Msg, State) -> {noreply, State} |
%%                                  {noreply, State, Timeout} |
%%                                  {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_cast(connect,  #st {
			 remote_addr = BTAddr,
			 channel = Channel,
			 mod = Mod,
			 func = Fun,
			 args = Arg
			} = St) ->

    %% Looong call that blocks for ever.
    case rfcomm:open(BTAddr, Channel) of
	{ok, ConnRef} ->
	    ?debug("bt_connection:init(connect): self():   ~p", [self()]),
	    ?debug("bt_connection:init(connect): BTAddr:   ~p", [BTAddr]),
	    ?debug("bt_connection:init(connect): Channel:  ~p", [Channel]),
	    ?debug("bt_connection:init(connect): Ref:      ~p", [ConnRef]),
	    ?debug("bt_connection:init(connect): Module:   ~p", [Mod]),
	    ?debug("bt_connection:init(connect): Function: ~p", [Fun]),
	    ?debug("bt_connection:init(connect): Arg:      ~p", [Arg]),

	    %% Add to managed connections,
	    bt_connection_manager:add_connection(BTAddr, Channel, self()),

	    Mod:Fun(self(), BTAddr, Channel, connected, Arg),

	    %% Update state with new connection
	    {noreply, St#st{
			rfcomm_ref = ConnRef
		       }};

	{ err, Error } ->
	    ?info("Failed to connect to ~p-~p", [ BTAddr, Channel]),
	    { stop, { connect_failed, Error}, St }
    end;

handle_cast({send, Data},  St) ->
    ?debug("~p:handle_call(send): Sending: ~p", 
	     [ ?MODULE, Data]),

    rfcomm:send(St#st.rfcomm_ref, Data),

    {noreply, St};

handle_cast(_Msg, State) ->
    ?warning("~p:handle_cast(): Unknown call: ~p", [ ?MODULE, _Msg]),
    {noreply, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling all non call/cast messages
%%
%% @spec handle_info(Info, State) -> {noreply, State} |
%%                                   {noreply, State, Timeout} |
%%                                   {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------

%% An accept reference we've setup now has accetpted an
%% incoming connection.
handle_info({rfcomm, _ARef, { accept, BTAddr, _ } }, 
	    #st { mod = Mod,
		  func = Fun,
		  args = Arg,
		  channel = Channel } = St)  ->

    ?info("~p:handle_info(): bt_connection from ~w:~w\n", 
	  [?MODULE, BTAddr,Channel]),
    
    Mod:Fun(self(), BTAddr, Channel, accepted, Arg),
    { noreply, St#st { remote_addr = BTAddr, 
		       channel = Channel } };


handle_info({rfcomm, _ConnRef, {data, Data}}, 
	    #st { remote_addr = BTAddr,
		  channel = Channel,
		  mod = Mod,
		  func = Fun,
		  args = Arg } = State) ->
    ?debug("~p:handle_info(data): Data: ~p", [ ?MODULE, Data]),
    ?info("~p:handle_info(data): From: ~p:~p ", [ ?MODULE, BTAddr, Channel]),
    ?info("~p:handle_info(data): ~p:~p -> ~p:~p", 
	  [ ?MODULE, BTAddr, Channel, Mod, Fun]),
    Self = self(),
    spawn(fun() -> Mod:Fun(Self, BTAddr, Channel, 
			   data, Data, Arg) end),

    {noreply, State};


handle_info({rfcomm_closed, ConnRef}, 
	    #st { remote_addr = BTAddr,
		  channel = Channel,
		  mod = Mod,
		  func = Fun,
		  args = Arg } = State) ->
    ?debug("~p:handle_info(tcp_closed): BTAddr: ~p:~p ", [ ?MODULE, BTAddr, Channel]),
    Mod:Fun(self(), BTAddr, Channel, closed, Arg),
    bt_connection_manager:delete_connection_by_pid(self()),
    rfcomm_close:close(ConnRef),
    {stop, normal, State};


handle_info({rfcomm_error, ConnRef}, 
	    #st { remote_addr = BTAddr,
		  channel = Channel,
		  mod = Mod,
		  func = Fun,
		  args = Arg} = State) ->

    ?debug("~p:handle_info(tcp_error): BTAddr: ~p:~p ", [ ?MODULE, BTAddr, Channel]),
    Mod:Fun(self(), BTAddr, Channel, error, Arg),
    rfcomm:close(ConnRef),
    bt_connection_manager:delete_connection_by_pid(self()),
    {stop, normal, State};


handle_info(_Info, State) ->
    ?warning("~p:handle_cast(): Unknown info: ~p", [ ?MODULE, _Info]),
    {noreply, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_server terminates
%% with Reason. The return value is ignored.
%%
%% @spec terminate(Reason, State) -> void()
%% @end
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
    ?debug("~p:terminate(): Reason: ~p ", [ ?MODULE, _Reason]),
    ok.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Convert process state when code is changed
%%
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
%% @end
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%%%===================================================================
%%% Internal functions
%%%===================================================================