-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ns: Add RelayConfigurationService WIP
- Loading branch information
1 parent
cff5de1
commit 2ecb67d
Showing
3 changed files
with
212 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
// Copyright © 2023 The Things Network Foundation, The Things Industries B.V. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package networkserver | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"go.thethings.network/lorawan-stack/v3/pkg/auth/rights" | ||
"go.thethings.network/lorawan-stack/v3/pkg/band" | ||
"go.thethings.network/lorawan-stack/v3/pkg/errors" | ||
"go.thethings.network/lorawan-stack/v3/pkg/frequencyplans" | ||
. "go.thethings.network/lorawan-stack/v3/pkg/networkserver/internal" | ||
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb" | ||
) | ||
|
||
var errRelayAlreadyExists = errors.DefineAlreadyExists("relay", "relay already exists") | ||
|
||
func validateRelaySecondChannel(secondCh *ttnpb.RelaySecondChannel, phy *band.Band, path ...string) error { | ||
if secondCh == nil { | ||
return nil | ||
} | ||
if _, ok := phy.DataRates[secondCh.DataRateIndex]; !ok { | ||
return newInvalidFieldValueError(strings.Join(append(path, "data_rate_index"), ".")) | ||
} | ||
inSubBand := false | ||
for _, sb := range phy.SubBands { | ||
if sb.MinFrequency >= secondCh.Frequency && secondCh.Frequency <= sb.MaxFrequency { | ||
inSubBand = true | ||
break | ||
} | ||
} | ||
if !inSubBand { | ||
return newInvalidFieldValueError(strings.Join(append(path, "frequency"), ".")) | ||
} | ||
return nil | ||
} | ||
|
||
func validateRelayConfigurationServed(served *ttnpb.RelayConfiguration_Served, phy *band.Band, path ...string) error { | ||
if served == nil { | ||
return nil | ||
} | ||
if err := validateRelaySecondChannel(served.SecondChannel, phy, append(path, "second_channel")...); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func validateRelayConfigurationServing(serving *ttnpb.RelayConfiguration_Serving, phy *band.Band, path ...string) error { | ||
if serving == nil { | ||
return nil | ||
} | ||
if err := validateRelaySecondChannel(serving.SecondChannel, phy, append(path, "second_channel")...); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func validateRelayConfiguration(conf *ttnpb.RelayConfiguration, phy *band.Band, path ...string) error { | ||
if conf == nil { | ||
return nil | ||
} | ||
switch mode := conf.Mode.(type) { | ||
case *ttnpb.RelayConfiguration_Served_: | ||
return validateRelayConfigurationServed(mode.Served, phy, append(path, "mode", "served")...) | ||
case *ttnpb.RelayConfiguration_Serving_: | ||
return validateRelayConfigurationServing(mode.Serving, phy, append(path, "mode", "serving")...) | ||
default: | ||
panic(fmt.Sprintf("unknown mode %T", mode)) | ||
} | ||
} | ||
|
||
type nsRelayConfigurationService struct { | ||
ttnpb.UnimplementedNsRelayConfigurationServiceServer | ||
|
||
devices DeviceRegistry | ||
frequencyPlans func(context.Context) (*frequencyplans.Store, error) | ||
} | ||
|
||
func relayParametersFromConfiguration(conf *ttnpb.RelayConfiguration) *ttnpb.RelayParameters { | ||
if conf == nil { | ||
return nil | ||
} | ||
switch mode := conf.Mode.(type) { | ||
case *ttnpb.RelayConfiguration_Served_: | ||
served := &ttnpb.ServedRelayParameters{ | ||
Backoff: mode.Served.Backoff, | ||
SecondChannel: mode.Served.SecondChannel, | ||
ServingDeviceId: mode.Served.ServingDeviceId, | ||
} | ||
switch mode := mode.Served.Mode.(type) { | ||
case *ttnpb.RelayConfiguration_Served_Always: | ||
served.Mode = &ttnpb.ServedRelayParameters_Always{ | ||
Always: mode.Always, | ||
} | ||
case *ttnpb.RelayConfiguration_Served_Dynamic: | ||
served.Mode = &ttnpb.ServedRelayParameters_Dynamic{ | ||
Dynamic: mode.Dynamic, | ||
} | ||
case *ttnpb.RelayConfiguration_Served_EndDeviceControlled: | ||
served.Mode = &ttnpb.ServedRelayParameters_EndDeviceControlled{ | ||
EndDeviceControlled: mode.EndDeviceControlled, | ||
} | ||
default: | ||
panic(fmt.Sprintf("unknown mode %T", mode)) | ||
} | ||
return &ttnpb.RelayParameters{ | ||
Mode: &ttnpb.RelayParameters_Served{ | ||
Served: served, | ||
}, | ||
} | ||
case *ttnpb.RelayConfiguration_Serving_: | ||
return &ttnpb.RelayParameters{ | ||
Mode: &ttnpb.RelayParameters_Serving{ | ||
Serving: &ttnpb.ServingRelayParameters{ | ||
SecondChannel: mode.Serving.SecondChannel, | ||
DefaultChannelIndex: mode.Serving.DefaultChannelIndex, | ||
CadPeriodicity: mode.Serving.CadPeriodicity, | ||
Limits: mode.Serving.Limits, | ||
}, | ||
}, | ||
} | ||
default: | ||
panic(fmt.Sprintf("unknown mode %T", mode)) | ||
} | ||
} | ||
|
||
// CreateRelay implements ttnpb.NsRelayConfigurationServiceServer. | ||
func (s *nsRelayConfigurationService) CreateRelay( | ||
ctx context.Context, req *ttnpb.CreateRelayRequest, | ||
) (*ttnpb.CreateRelayResponse, error) { | ||
if err := rights.RequireApplication( | ||
ctx, req.EndDeviceIds.ApplicationIds, ttnpb.Right_RIGHT_APPLICATION_DEVICES_WRITE, | ||
); err != nil { | ||
return nil, err | ||
} | ||
fps, err := s.frequencyPlans(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if _, _, err := s.devices.SetByID( | ||
ctx, | ||
req.EndDeviceIds.ApplicationIds, | ||
req.EndDeviceIds.DeviceId, | ||
[]string{ | ||
"frequency_plan_id", | ||
"lorawan_phy_version", | ||
"mac_settings.relay", | ||
"mac_state.desired_parameters", | ||
"pending_mac_state.desired_parameters", | ||
}, | ||
func(ctx context.Context, dev *ttnpb.EndDevice) (*ttnpb.EndDevice, []string, error) { | ||
if dev == nil { | ||
return nil, nil, errDeviceNotFound.New() | ||
} | ||
if dev.MacSettings.GetRelay() != nil { | ||
return nil, nil, errRelayAlreadyExists.New() | ||
} | ||
phy, err := DeviceBand(dev, fps) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
if err := validateRelayConfiguration(req.Configuration, phy, "configuration"); err != nil { | ||
return nil, nil, err | ||
} | ||
parameters := relayParametersFromConfiguration(req.Configuration) | ||
dev.MacSettings = &ttnpb.MACSettings{Relay: parameters} | ||
paths := []string{"mac_settings.relay"} | ||
for path, desiredParameters := range map[string]*ttnpb.MACParameters{ | ||
"mac_state.desired_parameters.relay": dev.MacState.GetDesiredParameters(), | ||
"pending_mac_state.desired_parameters.relay": dev.PendingMacState.GetDesiredParameters(), | ||
} { | ||
if desiredParameters == nil { | ||
continue | ||
} | ||
desiredParameters.Relay = parameters | ||
paths = ttnpb.AddFields(paths, path) | ||
} | ||
return dev, paths, nil | ||
}, | ||
); err != nil { | ||
logRegistryRPCError(ctx, err, "Failed to create relay") | ||
return nil, err | ||
} | ||
return &ttnpb.CreateRelayResponse{ | ||
Configuration: req.Configuration, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters