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

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

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

-define(INVOKE(Id, Op, Data), #'Invoke'{invokeId = Id, opcode = Op, argument = Data}).
-define(RESULT(Id, Op, Data), #'ReturnResult'{invokeId = Id, result = #'ReturnResult_result'{opcode = Op, result = Data}}).

%%%

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

handle_event({invoke, ?INVOKE(Id, ?STATUS, Data)}, _State) ->
	Status = pbx_status:decode(Data),
	Value = pbx_status:value(Status),
	io:format("Status: ~p~n", [Value]),
	case Value of
		normal ->
			pbx_pdu:notify({status, ?STATUS, pbx_status:encode()});
		_ ->
			ok
	end,
	{ok, Id};
handle_event({invoke, ?INVOKE(Id, ?EVENT, Data)}, _State) ->
	{ok, Event} = pbx_event:decode(Data),
	io:format("Event: ~p~n", [Event]),
	{ok, Id};
handle_event({invoke, ?INVOKE(Id, ?ESCAPE, Data)}, _State) ->
	Esc = pbx_escape:decode(Data),
	Priv = pbx_escape:privateData(Esc),
	io:format("Escape: ~p~n", [Priv]),
	{ok, Id};
handle_event({invoke, Invoke = #'Invoke'{invokeId = Id}}, _State) ->
	io:format("Invoke ~p~n", [Invoke]),
	{ok, Id};
handle_event({returnResult, ?RESULT(Id, ?MAKECALL, Data)}, _State) ->
	{ok, Result} = pbx_dial:decode(Data),
	io:format("Makecall ~p~n", [Result]),
	{ok, Id};
handle_event({returnResult, ?RESULT(Id, ?BUTTON, Data)}, _State) ->
	{ok, Result} = pbx_button:decode(Data),
	io:format("Button ~p~n", [Result]),
	{ok, Id};
handle_event({returnResult, ?RESULT(Id, ?MONITOR, Data)}, _State) ->
	{ok, Result} = pbx_monitor:decode(Data),
	io:format("Monitor ~p~n", [Result]),
	{ok, Id};
handle_event({returnResult, ?RESULT(Id, ?SNAPSHOT, Data)}, _State) ->
	{ok, Result} = pbx_snapshot:decode(Data),
	io:format("Snapshot ~p~n", [Result]),
	{ok, Id};
handle_event({returnResult, #'ReturnResult'{invokeId = Id, result = Result}}, _State) ->
	io:format("Result ~p~n", [Result]),
	{ok, Id};
handle_event({returnError, #'ReturnError'{invokeId = Id, errcode = ?ERROR, parameter = Data}}, _State) ->
	{ok, Error} = error:decode(Data),
	io:format("Error: ~p~n", [Error]),
	{ok, Id};
handle_event({returnError, ReturnError = #'ReturnError'{invokeId = Id}}, _State) ->
	io:format("ReturnError ~p~n", [ReturnError]),
	{ok, Id};
handle_event({reject, #'Reject'{invokeId = Id, problem = Problem}}, _State) ->
	io:format("Reject ~p~n", [Problem]),
	{ok, Id};
handle_event({invoke, Op, {ok, Data}}, State) ->
	NewState = next(State),
	Invoke = #'Invoke'{invokeId = NewState, opcode = Op, argument = Data},
	Reply = 'Remote-Operations-Generic-ROS-PDUs':encode('ROS', {invoke, Invoke}),
	pbx_conn:send(Reply),
	{ok, NewState};
handle_event({status, Op, {ok, Data}}, State) ->
	Result = #'ReturnResult_result'{opcode = Op, result = Data},
	ReturnResult = #'ReturnResult'{invokeId = State, result = Result},
	Reply = 'Remote-Operations-Generic-ROS-PDUs':encode('ROS', {returnResult, ReturnResult}),
	pbx_conn:send(Reply),
	{ok, State};
handle_event(_Event, State) ->
	{ok, State}.

handle_call({nextInvoke}, State) ->
	NewState = next(State),
	{ok, NewState, NewState};
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}.