1/*
2 *  Hamlib bindings - Amplifier interface
3 *  Copyright (c) 2001,2002 by Stephane Fillod
4 *  Copyright (c) 2020 by Michael Black W9MDB
5 *
6 *
7 *   This library is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU Lesser General Public
9 *   License as published by the Free Software Foundation; either
10 *   version 2.1 of the License, or (at your option) any later version.
11 *
12 *   This library is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 *   Lesser General Public License for more details.
16 *
17 *   You should have received a copy of the GNU Lesser General Public
18 *   License along with this library; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 *
21 */
22
23%inline %{
24
25typedef struct Amp {
26	AMP *amp;
27	struct amp_caps *caps;		/* shortcut to AMP->caps */
28	struct amp_state *state;	/* shortcut to AMP->state */
29	int error_status;
30	int do_exception;
31} Amp;
32
33%}
34
35/*
36 * declare wrapper method with 0,1,2 arguments besides AMP*
37 */
38#define AMPMETHOD0(f) void f () \
39				{ self->error_status = amp_##f(self->amp); }
40#define AMPMETHOD1(f, t1) void f (t1 _##t1) \
41				{ self->error_status = amp_##f(self->amp, _##t1); }
42#define AMPMETHOD2(f, t1, t2) void f (t1 _##t1##_1, t2 _##t2##_2) \
43				{ self->error_status = amp_##f(self->amp, _##t1##_1, _##t2##_2); }
44#define AMPMETHOD3(f, t1, t2, t3) void f (t1 _##t1##_1, t2 _##t2##_2, t3 _##t3##_3) \
45				{ self->error_status = amp_##f(self->amp, _##t1##_1, _##t2##_2, t3 _##t3##_3); }
46#define AMPMETHOD4(f, t1, t2, t3, t4) void f (t1 _##t1##_1, t2 _##t2##_3, t3 _##t3##_3, ##t4##_4) \
47				{ self->error_status = amp_##f(self->amp, _##t1##_1, _##t2##_3, t3 _##t3##_3, ##t4##_4); }
48
49%extend Amp {
50	Amp(amp_model_t amp_model) {
51		Amp *r;
52
53		r = (Amp*)malloc(sizeof(Amp));
54		if (!r)
55			return NULL;
56		r->amp = amp_init(amp_model);
57		if (!r->amp) {
58			free(r);
59			return NULL;
60		}
61		/* install shortcuts */
62		r->caps = r->amp->caps;
63		r->state = &r->amp->state;
64		r->do_exception = 0;    /* default is disabled */
65		r->error_status = RIG_OK;
66		return r;
67	}
68	~Amp () {
69		amp_cleanup(self->amp);
70		free(self);
71	}
72
73/*
74 * return code checking
75 */
76%exception {
77	arg1->error_status = RIG_OK;
78	$action
79	if (arg1->error_status != RIG_OK && arg1->do_exception)
80		SWIG_exception(SWIG_UnknownError, rigerror(arg1->error_status));
81}
82
83	AMPMETHOD0(open)
84	AMPMETHOD0(close)
85
86	AMPMETHOD1(reset, amp_reset_t)
87
88	AMPMETHOD1(token_lookup, const_char_string)	/* conf */
89
90  /* set functions */
91  AMPMETHOD1(set_freq, freq_t)
92  AMPMETHOD1(set_powerstat, powerstat_t)
93
94	void set_conf(const char *name, const char *val) {
95		token_t tok = amp_token_lookup(self->amp, name);
96		if (tok == RIG_CONF_END)
97			self->error_status = -RIG_EINVAL;
98		else
99			self->error_status = amp_set_conf(self->amp, tok, val);
100	}
101
102	AMPMETHOD2(set_conf, token_t, const_char_string)
103
104/*
105 * declare wrapper method with one output argument besides AMP*
106 */
107#define AMPMETHOD1VGET(f, t1) t1 f \
108        { t1 _##t1; self->error_status = amp_##f(self->amp, &_##t1); return _##t1; }
109
110
111  /* get functions */
112
113  const char *get_conf(token_t tok) {
114          static char s[128] = "";
115          self->error_status = amp_get_conf(self->amp, tok, s);
116          return s;
117  }
118
119  const char *get_conf(const char *name) {
120          token_t tok = amp_token_lookup(self->amp, name);
121          static char s[128] = "";
122          if (tok == RIG_CONF_END)
123                  self->error_status = -RIG_EINVAL;
124          else
125                  self->error_status = amp_get_conf(self->amp, tok, s);
126          return s;
127  }
128
129  const char * get_info(void) {
130          const char *s;
131          s = amp_get_info(self->amp);
132          self->error_status = s ? RIG_OK : -RIG_EINVAL;
133          return s;
134  }
135
136};
137%{
138
139  void Amp_get_freq(Amp *self, freq_t *freq)
140  {
141    self->error_status = amp_get_freq(self->amp, freq);
142  }
143  void Amp_get_powerstat(Amp *self, powerstat_t *status)
144  {
145    self->error_status = amp_get_powerstat(self->amp, status);
146  }
147%}
148