src: Add DMA localagent
[barometer.git] / src / dma / vendor / github.com / libvirt / libvirt-go / node_device.go
1 /*
2  * This file is part of the libvirt-go project
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  *
22  * Copyright (c) 2013 Alex Zorin
23  * Copyright (C) 2016 Red Hat, Inc.
24  *
25  */
26
27 package libvirt
28
29 /*
30 #cgo pkg-config: libvirt
31 #include <stdlib.h>
32 #include "node_device_wrapper.h"
33 */
34 import "C"
35
36 import (
37         "unsafe"
38 )
39
40 type NodeDeviceEventID int
41
42 const (
43         NODE_DEVICE_EVENT_ID_LIFECYCLE = NodeDeviceEventID(C.VIR_NODE_DEVICE_EVENT_ID_LIFECYCLE)
44         NODE_DEVICE_EVENT_ID_UPDATE    = NodeDeviceEventID(C.VIR_NODE_DEVICE_EVENT_ID_UPDATE)
45 )
46
47 type NodeDeviceEventLifecycleType int
48
49 const (
50         NODE_DEVICE_EVENT_CREATED = NodeDeviceEventLifecycleType(C.VIR_NODE_DEVICE_EVENT_CREATED)
51         NODE_DEVICE_EVENT_DELETED = NodeDeviceEventLifecycleType(C.VIR_NODE_DEVICE_EVENT_DELETED)
52 )
53
54 type NodeDevice struct {
55         ptr C.virNodeDevicePtr
56 }
57
58 // See also https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceFree
59 func (n *NodeDevice) Free() error {
60         var err C.virError
61         ret := C.virNodeDeviceFreeWrapper(n.ptr, &err)
62         if ret == -1 {
63                 return makeError(&err)
64         }
65         return nil
66 }
67
68 // See also https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceRef
69 func (c *NodeDevice) Ref() error {
70         var err C.virError
71         ret := C.virNodeDeviceRefWrapper(c.ptr, &err)
72         if ret == -1 {
73                 return makeError(&err)
74         }
75         return nil
76 }
77
78 // See also https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceDestroy
79 func (n *NodeDevice) Destroy() error {
80         var err C.virError
81         result := C.virNodeDeviceDestroyWrapper(n.ptr, &err)
82         if result == -1 {
83                 return makeError(&err)
84         }
85         return nil
86 }
87
88 // See also https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceReset
89 func (n *NodeDevice) Reset() error {
90         var err C.virError
91         result := C.virNodeDeviceResetWrapper(n.ptr, &err)
92         if result == -1 {
93                 return makeError(&err)
94         }
95         return nil
96 }
97
98 // See also https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceDettach
99 func (n *NodeDevice) Detach() error {
100         var err C.virError
101         result := C.virNodeDeviceDettachWrapper(n.ptr, &err)
102         if result == -1 {
103                 return makeError(&err)
104         }
105         return nil
106 }
107
108 // See also https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceDetachFlags
109 func (n *NodeDevice) DetachFlags(driverName string, flags uint32) error {
110         cDriverName := C.CString(driverName)
111         defer C.free(unsafe.Pointer(cDriverName))
112         var err C.virError
113         result := C.virNodeDeviceDetachFlagsWrapper(n.ptr, cDriverName, C.uint(flags), &err)
114         if result == -1 {
115                 return makeError(&err)
116         }
117         return nil
118 }
119
120 // See also https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceReAttach
121 func (n *NodeDevice) ReAttach() error {
122         var err C.virError
123         result := C.virNodeDeviceReAttachWrapper(n.ptr, &err)
124         if result == -1 {
125                 return makeError(&err)
126         }
127         return nil
128 }
129
130 // See also https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceGetName
131 func (n *NodeDevice) GetName() (string, error) {
132         var err C.virError
133         name := C.virNodeDeviceGetNameWrapper(n.ptr, &err)
134         if name == nil {
135                 return "", makeError(&err)
136         }
137         return C.GoString(name), nil
138 }
139
140 // See also https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceGetXMLDesc
141 func (n *NodeDevice) GetXMLDesc(flags uint32) (string, error) {
142         var err C.virError
143         result := C.virNodeDeviceGetXMLDescWrapper(n.ptr, C.uint(flags), &err)
144         if result == nil {
145                 return "", makeError(&err)
146         }
147         xml := C.GoString(result)
148         C.free(unsafe.Pointer(result))
149         return xml, nil
150 }
151
152 // See also https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceGetParent
153 func (n *NodeDevice) GetParent() (string, error) {
154         var err C.virError
155         result := C.virNodeDeviceGetParentWrapper(n.ptr, &err)
156         if result == nil {
157                 return "", makeError(&err)
158         }
159         defer C.free(unsafe.Pointer(result))
160         return C.GoString(result), nil
161 }
162
163 // See also https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceNumOfCaps
164 func (p *NodeDevice) NumOfCaps() (int, error) {
165         var err C.virError
166         result := int(C.virNodeDeviceNumOfCapsWrapper(p.ptr, &err))
167         if result == -1 {
168                 return 0, makeError(&err)
169         }
170         return result, nil
171 }
172
173 // See also https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceListCaps
174 func (p *NodeDevice) ListCaps() ([]string, error) {
175         const maxCaps = 1024
176         var names [maxCaps](*C.char)
177         namesPtr := unsafe.Pointer(&names)
178         var err C.virError
179         numCaps := C.virNodeDeviceListCapsWrapper(
180                 p.ptr,
181                 (**C.char)(namesPtr),
182                 maxCaps, &err)
183         if numCaps == -1 {
184                 return nil, makeError(&err)
185         }
186         goNames := make([]string, numCaps)
187         for k := 0; k < int(numCaps); k++ {
188                 goNames[k] = C.GoString(names[k])
189                 C.free(unsafe.Pointer(names[k]))
190         }
191         return goNames, nil
192 }