|
| 1 | +// Copyright (c) 2025 Canonical Ltd |
| 2 | +// |
| 3 | +// This program is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License version 3 as |
| 5 | +// published by the Free Software Foundation. |
| 6 | +// |
| 7 | +// This program is distributed in the hope that it will be useful, |
| 8 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | +// GNU General Public License for more details. |
| 11 | +// |
| 12 | +// You should have received a copy of the GNU General Public License |
| 13 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | + |
| 15 | +package pairingstate |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "fmt" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + // Registered pairing controller extensions. |
| 24 | + controllerExtensions = map[string]ControllerExtension{} |
| 25 | +) |
| 26 | + |
| 27 | +// PairingAccessor provides access to the pairing manager functions to |
| 28 | +// control the pairing window. |
| 29 | +type PairingAccessor interface { |
| 30 | + EnablePairing() error |
| 31 | + DisablePairing() error |
| 32 | +} |
| 33 | + |
| 34 | +type ControllerConfig = any |
| 35 | + |
| 36 | +type ControllerExtension interface { |
| 37 | + // UnmarshalConfig unmarshals the controller configuration JSON into |
| 38 | + // a concrete backing type, and returns a pointer to the instance. |
| 39 | + UnmarshalConfig(json.RawMessage) (ControllerConfig, error) |
| 40 | + |
| 41 | + // NewController creates a new pairing manager controller. |
| 42 | + NewController(PairingAccessor, ControllerConfig) (Controller, error) |
| 43 | +} |
| 44 | + |
| 45 | +// RegisterPairingController registers a pairing controller extension. |
| 46 | +func RegisterPairingController(name string, ext ControllerExtension) { |
| 47 | + if _, ok := controllerExtensions[name]; ok { |
| 48 | + panic(fmt.Sprintf("internal error: controller %q already registered", name)) |
| 49 | + } |
| 50 | + controllerExtensions[name] = ext |
| 51 | +} |
| 52 | + |
| 53 | +// UnregisterPairingController removed a registered pairing controller extension. |
| 54 | +func UnregisterPairingController(name string) { |
| 55 | + delete(controllerExtensions, name) |
| 56 | +} |
| 57 | + |
| 58 | +type PairingControllerConfigs struct { |
| 59 | + PairingControllers map[string]ControllerConfig `json:"pairing-controllers"` |
| 60 | +} |
| 61 | + |
| 62 | +func (c *PairingControllerConfigs) UnmarshalJSON(data []byte) error { |
| 63 | + var rawCfg struct { |
| 64 | + PairingControllers map[string]json.RawMessage `json:"pairing-controllers"` |
| 65 | + } |
| 66 | + if err := json.Unmarshal(data, &rawCfg); err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + c.PairingControllers = make(map[string]ControllerConfig) |
| 71 | + for name, rawJSON := range rawCfg.PairingControllers { |
| 72 | + ext, ok := controllerExtensions[name] |
| 73 | + if !ok { |
| 74 | + // Ignore unsupported controller name. |
| 75 | + continue |
| 76 | + } |
| 77 | + ctrl, err := ext.UnmarshalConfig(rawJSON) |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("cannot unmarshal pairing controller %q config: %w", name, err) |
| 80 | + } |
| 81 | + c.PairingControllers[name] = ctrl |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func (c *PairingControllerConfigs) CreateControllers(p PairingAccessor) ([]Controller, error) { |
| 87 | + controllers := []Controller{} |
| 88 | + for name, config := range c.PairingControllers { |
| 89 | + ext, ok := controllerExtensions[name] |
| 90 | + if !ok { |
| 91 | + // Ignore unsupported controller name. |
| 92 | + continue |
| 93 | + } |
| 94 | + controller, err := ext.NewController(p, config) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + controllers = append(controllers, controller) |
| 99 | + } |
| 100 | + return controllers, nil |
| 101 | +} |
0 commit comments