changeState attempts to change the device state to match want.
(want deviceState)
| 141 | |
| 142 | // changeState attempts to change the device state to match want. |
| 143 | func (device *Device) changeState(want deviceState) (err error) { |
| 144 | device.state.Lock() |
| 145 | defer device.state.Unlock() |
| 146 | old := device.deviceState() |
| 147 | if old == deviceStateClosed { |
| 148 | // once closed, always closed |
| 149 | device.Log.Verbosef("Interface closed, ignored requested state %s", want) |
| 150 | return nil |
| 151 | } |
| 152 | switch want { |
| 153 | case old: |
| 154 | return nil |
| 155 | case deviceStateUp: |
| 156 | device.state.state.Store(uint32(deviceStateUp)) |
| 157 | err = device.upLocked() |
| 158 | if err == nil { |
| 159 | break |
| 160 | } |
| 161 | fallthrough // up failed; bring the device all the way back down |
| 162 | case deviceStateDown: |
| 163 | device.state.state.Store(uint32(deviceStateDown)) |
| 164 | errDown := device.downLocked() |
| 165 | if err == nil { |
| 166 | err = errDown |
| 167 | } |
| 168 | } |
| 169 | device.Log.Verbosef("Interface state was %s, requested %s, now %s", old, want, device.deviceState()) |
| 170 | return |
| 171 | } |
| 172 | |
| 173 | // upLocked attempts to bring the device up and reports whether it succeeded. |
| 174 | // The caller must hold device.state.mu and is responsible for updating device.state.state. |
no test coverage detected