Debian Codegraph @

gen/generator/tpl/api.go.tpl

raw
1 // Code generated by go-bluetooth generator DO NOT EDIT.
2
3 package {{.Package}}
4 {{- $InterfaceName := .InterfaceName}}
5 {{- $ExposeProperties := .ExposeProperties}}
6
7 {{.Imports}}
8
9 var {{.InterfaceName}}Interface = "{{.Api.Interface}}"
10
11 {{range .Constructors}}
12 // New{{$InterfaceName}}{{.Role}} create a new instance of {{$InterfaceName}}
13 {{.ArgsDocs}}
14 func New{{$InterfaceName}}{{.Role}}({{.Args}}) (*{{$InterfaceName}}, error) {
15 a := new({{$InterfaceName}})
16 a.client = bluez.NewClient(
17 &bluez.Config{
18 Name: {{.Service}},
19 Iface: {{$InterfaceName}}Interface,
20 Path: dbus.ObjectPath({{.ObjectPath}}),
21 Bus: bluez.SystemBus,
22 },
23 )
24 {{- if $ExposeProperties }}
25 a.Properties = new({{$InterfaceName}}Properties)
26
27 _, err := a.GetProperties()
28 if err != nil {
29 return nil, err
30 }
31 {{- end}}
32 return a, nil
33 }
34 {{- end}}
35
36 /*
37 {{.InterfaceName}} {{.Api.Title}}
38 {{.Api.Description}}
39 */
40 type {{.InterfaceName}} struct {
41 client *bluez.Client
42 propertiesSignal chan *dbus.Signal
43 objectManagerSignal chan *dbus.Signal
44 objectManager *bluez.ObjectManager
45 Properties *{{.InterfaceName}}Properties
46 watchPropertiesChannel chan *dbus.Signal
47 }
48
49 // {{.InterfaceName}}Properties contains the exposed properties of an interface
50 type {{.InterfaceName}}Properties struct {
51 lock sync.RWMutex `dbus:"ignore"`
52 {{- range .Properties }}
53
54 /*
55 {{.Property.Name}} {{.Property.Docs}}
56 */
57 {{.Property.Name}} {{.Property.Type}}
58 {{- end}}
59 }
60
61 //Lock access to properties
62 func (p *{{.InterfaceName}}Properties) Lock() {
63 p.lock.Lock()
64 }
65
66 //Unlock access to properties
67 func (p *{{.InterfaceName}}Properties) Unlock() {
68 p.lock.Unlock()
69 }
70
71 {{- range .Properties }}
72 {{- if not .ReadOnly }}
73
74 // Set{{.Property.Name}} set {{.Property.Name}} value
75 func (a *{{$InterfaceName}}) Set{{.Property.Name}}(v {{.RawType}}) error {
76 return a.SetProperty("{{.Property.Name}}", v)
77 }
78 {{- end}}
79
80 {{- if not .WriteOnly }}
81
82 // Get{{.Property.Name}} get {{.Property.Name}} value
83 func (a *{{$InterfaceName}}) Get{{.Property.Name}}() ({{.RawType}}, error) {
84 v, err := a.GetProperty("{{.Property.Name}}")
85 if err != nil {
86 return {{.RawTypeInitializer}}, err
87 }
88 return v.Value().({{.RawType}}), nil
89 }
90 {{- end}}
91 {{- end}}
92
93 // Close the connection
94 func (a *{{.InterfaceName}}) Close() {
95 {{- if $ExposeProperties }}
96 a.unregisterPropertiesSignal()
97 {{- end}}
98 a.client.Disconnect()
99 }
100
101 // Path return {{.InterfaceName}} object path
102 func (a *{{.InterfaceName}}) Path() dbus.ObjectPath {
103 return a.client.Config.Path
104 }
105
106 // Client return {{.InterfaceName}} dbus client
107 func (a *{{.InterfaceName}}) Client() *bluez.Client {
108 return a.client
109 }
110
111 // Interface return {{.InterfaceName}} interface
112 func (a *{{.InterfaceName}}) Interface() string {
113 return a.client.Config.Iface
114 }
115
116 // GetObjectManagerSignal return a channel for receiving updates from the ObjectManager
117 func (a *{{.InterfaceName}}) GetObjectManagerSignal() (chan *dbus.Signal, func(), error) {
118
119 if a.objectManagerSignal == nil {
120 if a.objectManager == nil {
121 om, err := bluez.GetObjectManager()
122 if err != nil {
123 return nil, nil, err
124 }
125 a.objectManager = om
126 }
127
128 s, err := a.objectManager.Register()
129 if err != nil {
130 return nil, nil, err
131 }
132 a.objectManagerSignal = s
133 }
134
135 cancel := func() {
136 if a.objectManagerSignal == nil {
137 return
138 }
139 a.objectManagerSignal <- nil
140 a.objectManager.Unregister(a.objectManagerSignal)
141 a.objectManagerSignal = nil
142 }
143
144 return a.objectManagerSignal, cancel, nil
145 }
146
147 {{- if .ExposeProperties }}
148
149 // ToMap convert a {{.InterfaceName}}Properties to map
150 func (a *{{.InterfaceName}}Properties) ToMap() (map[string]interface{}, error) {
151 return props.ToMap(a), nil
152 }
153
154 // FromMap convert a map to an {{.InterfaceName}}Properties
155 func (a *{{.InterfaceName}}Properties) FromMap(props map[string]interface{}) (*{{.InterfaceName}}Properties, error) {
156 props1 := map[string]dbus.Variant{}
157 for k, val := range props {
158 props1[k] = dbus.MakeVariant(val)
159 }
160 return a.FromDBusMap(props1)
161 }
162
163 // FromDBusMap convert a map to an {{.InterfaceName}}Properties
164 func (a *{{.InterfaceName}}Properties) FromDBusMap(props map[string]dbus.Variant) (*{{.InterfaceName}}Properties, error) {
165 s := new({{.InterfaceName}}Properties)
166 err := util.MapToStruct(s, props)
167 return s, err
168 }
169
170 // ToProps return the properties interface
171 func (a *{{.InterfaceName}}) ToProps() bluez.Properties {
172 return a.Properties
173 }
174
175 // GetWatchPropertiesChannel return the dbus channel to receive properties interface
176 func (a *{{.InterfaceName}}) GetWatchPropertiesChannel() chan *dbus.Signal {
177 return a.watchPropertiesChannel
178 }
179
180 // SetWatchPropertiesChannel set the dbus channel to receive properties interface
181 func (a *{{.InterfaceName}}) SetWatchPropertiesChannel(c chan *dbus.Signal) {
182 a.watchPropertiesChannel = c
183 }
184
185 // GetProperties load all available properties
186 func (a *{{.InterfaceName}}) GetProperties() (*{{.InterfaceName}}Properties, error) {
187 a.Properties.Lock()
188 err := a.client.GetProperties(a.Properties)
189 a.Properties.Unlock()
190 return a.Properties, err
191 }
192
193 // SetProperty set a property
194 func (a *{{.InterfaceName}}) SetProperty(name string, value interface{}) error {
195 return a.client.SetProperty(name, value)
196 }
197
198 // GetProperty get a property
199 func (a *{{.InterfaceName}}) GetProperty(name string) (dbus.Variant, error) {
200 return a.client.GetProperty(name)
201 }
202
203 // GetPropertiesSignal return a channel for receiving udpdates on property changes
204 func (a *{{.InterfaceName}}) GetPropertiesSignal() (chan *dbus.Signal, error) {
205
206 if a.propertiesSignal == nil {
207 s, err := a.client.Register(a.client.Config.Path, bluez.PropertiesInterface)
208 if err != nil {
209 return nil, err
210 }
211 a.propertiesSignal = s
212 }
213
214 return a.propertiesSignal, nil
215 }
216
217 // Unregister for changes signalling
218 func (a *{{.InterfaceName}}) unregisterPropertiesSignal() {
219 if a.propertiesSignal != nil {
220 a.propertiesSignal <- nil
221 a.propertiesSignal = nil
222 }
223 }
224
225 // WatchProperties updates on property changes
226 func (a *{{.InterfaceName}}) WatchProperties() (chan *bluez.PropertyChanged, error) {
227 return bluez.WatchProperties(a)
228 }
229
230 func (a *{{.InterfaceName}}) UnwatchProperties(ch chan *bluez.PropertyChanged) error {
231 return bluez.UnwatchProperties(a, ch)
232 }
233
234 {{- end}}
235
236 {{- range .Methods}}
237
238 /*
239 {{.Name}} {{.Docs}}
240 */
241 func (a *{{$InterfaceName}}) {{.Name}}({{.ArgsList}}) {{.Method.ReturnType}} {
242 {{- if .SingleReturn}}
243 return a.client.Call("{{.Name}}", 0, {{.ParamsList}}).Store()
244 {{- else}}
245 {{.ReturnVarsDefinition}}
246 err := a.client.Call("{{.Name}}", 0, {{.ParamsList}}).Store({{.ReturnVarsRefs}})
247 return {{.ReturnVarsList}}, err{{end}}
248 }
249 {{- end}}