summaryrefslogtreecommitdiff
path: root/src/pbx_rose.erl
blob: 63282263d4e4832a9380db5e057a4e9b68b48b34 (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
-module(pbx_rose).
-behaviour(gen_event).

-export([init/1, handle_event/2, handle_call/2, handle_info/2,
	 terminate/2, code_change/3]).
-export([invoke/2]).

-include("opcodes.hrl").
-include("Remote-Operations-Generic-ROS-PDUs.hrl").

decode({invoke, #'Invoke'{invokeId = Id, opcode = Op, argument = Data}}) ->
	pbx_invoke:set(Id),
	case Op of
		?MAKECALL ->
			ok;
		?BUTTON ->
			ok;
		?EVENT ->
			{ok, Event} = pbx_event:decode(Data),
			io:format("Event: ~p~n", [Event]),
			ok;
		?ESCAPE ->
			pbx_escape:privateData(pbx_escape:decode(Data));
		?MONITOR ->
			ok;
		?SNAPSHOT ->
			ok;
		?STATUS ->
			%io:format("Status: ~p~n", [pbx_status:value(Data)]),
			%%% pbx_conn:send(return(Id, Op, pbx_status:encode()));
			ok;
		_ ->
			error
	end;
decode({returnResult, #'ReturnResult'{invokeId = Id, result = Data}}) ->
	pbx_invoke:set(Id),
	decode(Data);
decode({returnError, #'ReturnError'{invokeId = Id, errcode = Op, parameter = Data}}) ->
	pbx_invoke:set(Id),
	case Op of
		?ERROR ->
			{ok, Error} = error:decode(Data),
			io:format("Error: ~p~n", [Error])
	end,
	error;
decode({reject, #'Reject'{invokeId = Id, problem = Problem}}) ->
	pbx_invoke:set(Id),
	io:format("Problem: ~p~n", [Problem]),
	error;
decode(#'ReturnResult_result'{opcode = Op, result = Data}) ->
	case Op of
		?MAKECALL ->
			{ok, Result} = pbx_dial:decode(Data),
			io:format("Make call: ~p~n", [Result]);
		?BUTTON ->
			{ok, List} = pbx_button:decode(Data),
			io:format("List: ~p~n", [List]);
		?MONITOR ->
			{ok, Status} = pbx_monitor:decode(Data),
			io:format("Result: ~p~n", [Status]);
		?SNAPSHOT ->
			{ok, Status} = pbx_snapshot:decode(Data),
			io:format("Result: ~p~n", [Status]);
		_ ->
		io:format("Result: ~p~n", [Data])
	end,
	ok;
decode(<<Data/binary>>) ->
	{ok, Pdu} = 'Remote-Operations-Generic-ROS-PDUs':decode('ROS', Data),
	decode(Pdu).

%%%

init(_Args) ->
	{ok, {present, 0}}.

handle_event({invoke, Invoke = #'Invoke'{opcode = ?STATUS}}, _State) ->
	Status = pbx_status:decode(Invoke#'Invoke'.argument),
	case pbx_status:value(Status) of
		normal ->
			reply(Invoke, pbx_status:encode());
		_ ->
			ok
	end,
	{ok, Invoke#'Invoke'.invokeId};
handle_event({invoke, Invoke = #'Invoke'{}}, _State) ->
	io:format("Invoke ~p~n", [Invoke]),
	{ok, Invoke#'Invoke'.invokeId};
handle_event({returnResult, ReturnResult = #'ReturnResult'{}}, _State) ->
	io:format("ReturnResult ~p~n", [ReturnResult]),
	{ok, ReturnResult#'ReturnResult'.invokeId};
handle_event({returnError, ReturnError = #'ReturnError'{}}, _State) ->
	io:format("ReturnError ~p~n", [ReturnError]),
	{ok, ReturnError#'ReturnError'.invokeId};
handle_event({reject, Reject = #'Reject'{problem = Problem}}, _State) ->
	io:format("Reject ~p~n", [Problem]),
	{ok, Reject#'Reject'.invokeId};
handle_event(_Event, State) ->
	{ok, State}.

handle_call(nextInvoke, State) ->
	{ok, next(State), next(State)};
handle_call(_Request, State) ->
	{ok, ok, State}.

handle_info(_Info, State) ->
	{ok, State}.

terminate(_Reason, _State) ->
	ok.

code_change(_OldVsn, State, _Extra) ->
	{ok, State}.

%%%

next({present, N}) ->
	{present, N+1}.

reply(Invoke = #'Invoke'{}, Data) ->
	pbx_conn:send(return(Invoke, Data)).

return(#'Invoke'{invokeId = Id, opcode = Op}, {ok, Data}) ->
	Result = #'ReturnResult_result'{opcode = Op, result = Data},
	ReturnResult = #'ReturnResult'{invokeId = Id, result = Result},
	'Remote-Operations-Generic-ROS-PDUs':encode('ROS', {returnResult,
							    ReturnResult}).

invoke(Op, {ok, Data}) ->
	Id = pbx_pdu:call(?MODULE, nextInvoke),
	Invoke = #'Invoke'{invokeId = Id, opcode = Op, argument = Data},
	'Remote-Operations-Generic-ROS-PDUs':encode('ROS', {invoke, Invoke}).