Skip to content

Commit 6b79a1b

Browse files
committed
lora/lorawan: refactor shared functionality for ChannelUS, etc into embedded type
named channel, do the same for RegionSettings, and then change RegionSettings to just Settings to avoid the redundant naming. Signed-off-by: deadprogram <[email protected]>
1 parent 7dd1067 commit 6b79a1b

File tree

7 files changed

+105
-161
lines changed

7 files changed

+105
-161
lines changed

lora/lorawan/adaptor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ const (
3030
var (
3131
ActiveRadio lora.Radio
3232
Retries = 15
33-
regionSettings region.RegionSettings
33+
regionSettings region.Settings
3434
)
3535

3636
// UseRegionSettings sets current Lorawan Regional parameters
37-
func UseRegionSettings(rs region.RegionSettings) {
37+
func UseRegionSettings(rs region.Settings) {
3838
regionSettings = rs
3939
}
4040

lora/lorawan/region/au915.go

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,75 +8,40 @@ const (
88
)
99

1010
type ChannelAU struct {
11-
frequency uint32
12-
bandwidth uint8
13-
spreadingFactor uint8
14-
codingRate uint8
15-
preambleLength uint16
16-
txPowerDBm int8
11+
channel
1712
}
1813

19-
// Getter functions
20-
func (c *ChannelAU) Frequency() uint32 { return c.frequency }
21-
func (c *ChannelAU) Bandwidth() uint8 { return c.bandwidth }
22-
func (c *ChannelAU) SpreadingFactor() uint8 { return c.spreadingFactor }
23-
func (c *ChannelAU) CodingRate() uint8 { return c.codingRate }
24-
func (c *ChannelAU) PreambleLength() uint16 { return c.preambleLength }
25-
func (c *ChannelAU) TxPowerDBm() int8 { return c.txPowerDBm }
26-
27-
// Set functions
28-
func (c *ChannelAU) SetFrequency(v uint32) { c.frequency = v }
29-
func (c *ChannelAU) SetBandwidth(v uint8) { c.bandwidth = v }
30-
func (c *ChannelAU) SetSpreadingFactor(v uint8) { c.spreadingFactor = v }
31-
func (c *ChannelAU) SetCodingRate(v uint8) { c.codingRate = v }
32-
func (c *ChannelAU) SetPreambleLength(v uint16) { c.preambleLength = v }
33-
func (c *ChannelAU) SetTxPowerDBm(v int8) { c.txPowerDBm = v }
34-
3514
func (c *ChannelAU) Next() bool {
3615
return false
3716
}
3817

39-
type RegionSettingsAU915 struct {
40-
joinRequestChannel *ChannelAU
41-
joinAcceptChannel *ChannelAU
42-
uplinkChannel *ChannelAU
18+
type SettingsAU915 struct {
19+
settings
4320
}
4421

45-
func AU915() *RegionSettingsAU915 {
46-
return &RegionSettingsAU915{
47-
joinRequestChannel: &ChannelAU{lora.MHz_916_8,
22+
func AU915() *SettingsAU915 {
23+
return &SettingsAU915{settings: settings{
24+
joinRequestChannel: &ChannelAU{channel: channel{lora.MHz_916_8,
4825
lora.Bandwidth_125_0,
4926
lora.SpreadingFactor9,
5027
lora.CodingRate4_5,
5128
AU915_DEFAULT_PREAMBLE_LEN,
52-
AU915_DEFAULT_TX_POWER_DBM},
53-
joinAcceptChannel: &ChannelAU{lora.MHz_923_3,
29+
AU915_DEFAULT_TX_POWER_DBM}},
30+
joinAcceptChannel: &ChannelAU{channel: channel{lora.MHz_923_3,
5431
lora.Bandwidth_500_0,
5532
lora.SpreadingFactor9,
5633
lora.CodingRate4_5,
5734
AU915_DEFAULT_PREAMBLE_LEN,
58-
AU915_DEFAULT_TX_POWER_DBM},
59-
uplinkChannel: &ChannelAU{lora.MHz_916_8,
35+
AU915_DEFAULT_TX_POWER_DBM}},
36+
uplinkChannel: &ChannelAU{channel: channel{lora.MHz_916_8,
6037
lora.Bandwidth_125_0,
6138
lora.SpreadingFactor9,
6239
lora.CodingRate4_5,
6340
AU915_DEFAULT_PREAMBLE_LEN,
64-
AU915_DEFAULT_TX_POWER_DBM},
65-
}
41+
AU915_DEFAULT_TX_POWER_DBM}},
42+
}}
6643
}
6744

6845
func Next(c *ChannelAU) bool {
6946
return false
7047
}
71-
72-
func (r *RegionSettingsAU915) JoinRequestChannel() Channel {
73-
return r.joinRequestChannel
74-
}
75-
76-
func (r *RegionSettingsAU915) JoinAcceptChannel() Channel {
77-
return r.joinAcceptChannel
78-
}
79-
80-
func (r *RegionSettingsAU915) UplinkChannel() Channel {
81-
return r.uplinkChannel
82-
}

lora/lorawan/region/channel.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package region
2+
3+
type Channel interface {
4+
Next() bool
5+
Frequency() uint32
6+
Bandwidth() uint8
7+
SpreadingFactor() uint8
8+
CodingRate() uint8
9+
PreambleLength() uint16
10+
TxPowerDBm() int8
11+
SetFrequency(v uint32)
12+
SetBandwidth(v uint8)
13+
SetSpreadingFactor(v uint8)
14+
SetCodingRate(v uint8)
15+
SetPreambleLength(v uint16)
16+
SetTxPowerDBm(v int8)
17+
}
18+
19+
type channel struct {
20+
frequency uint32
21+
bandwidth uint8
22+
spreadingFactor uint8
23+
codingRate uint8
24+
preambleLength uint16
25+
txPowerDBm int8
26+
}
27+
28+
// Getter functions
29+
func (c *channel) Frequency() uint32 { return c.frequency }
30+
func (c *channel) Bandwidth() uint8 { return c.bandwidth }
31+
func (c *channel) SpreadingFactor() uint8 { return c.spreadingFactor }
32+
func (c *channel) CodingRate() uint8 { return c.codingRate }
33+
func (c *channel) PreambleLength() uint16 { return c.preambleLength }
34+
func (c *channel) TxPowerDBm() int8 { return c.txPowerDBm }
35+
36+
// Set functions
37+
func (c *channel) SetFrequency(v uint32) { c.frequency = v }
38+
func (c *channel) SetBandwidth(v uint8) { c.bandwidth = v }
39+
func (c *channel) SetSpreadingFactor(v uint8) { c.spreadingFactor = v }
40+
func (c *channel) SetCodingRate(v uint8) { c.codingRate = v }
41+
func (c *channel) SetPreambleLength(v uint16) { c.preambleLength = v }
42+
func (c *channel) SetTxPowerDBm(v int8) { c.txPowerDBm = v }

lora/lorawan/region/eu868.go

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,71 +8,36 @@ const (
88
)
99

1010
type ChannelEU struct {
11-
frequency uint32
12-
bandwidth uint8
13-
spreadingFactor uint8
14-
codingRate uint8
15-
preambleLength uint16
16-
txPowerDBm int8
11+
channel
1712
}
1813

19-
// Getter functions
20-
func (c *ChannelEU) Frequency() uint32 { return c.frequency }
21-
func (c *ChannelEU) Bandwidth() uint8 { return c.bandwidth }
22-
func (c *ChannelEU) SpreadingFactor() uint8 { return c.spreadingFactor }
23-
func (c *ChannelEU) CodingRate() uint8 { return c.codingRate }
24-
func (c *ChannelEU) PreambleLength() uint16 { return c.preambleLength }
25-
func (c *ChannelEU) TxPowerDBm() int8 { return c.txPowerDBm }
26-
27-
// Set functions
28-
func (c *ChannelEU) SetFrequency(v uint32) { c.frequency = v }
29-
func (c *ChannelEU) SetBandwidth(v uint8) { c.bandwidth = v }
30-
func (c *ChannelEU) SetSpreadingFactor(v uint8) { c.spreadingFactor = v }
31-
func (c *ChannelEU) SetCodingRate(v uint8) { c.codingRate = v }
32-
func (c *ChannelEU) SetPreambleLength(v uint16) { c.preambleLength = v }
33-
func (c *ChannelEU) SetTxPowerDBm(v int8) { c.txPowerDBm = v }
34-
3514
func (c *ChannelEU) Next() bool {
3615
return false
3716
}
3817

39-
type RegionSettingsEU868 struct {
40-
joinRequestChannel *ChannelEU
41-
joinAcceptChannel *ChannelEU
42-
uplinkChannel *ChannelEU
18+
type SettingsEU868 struct {
19+
settings
4320
}
4421

45-
func EU868() *RegionSettingsEU868 {
46-
return &RegionSettingsEU868{
47-
joinRequestChannel: &ChannelEU{lora.MHz_868_1,
22+
func EU868() *SettingsEU868 {
23+
return &SettingsEU868{settings: settings{
24+
joinRequestChannel: &ChannelEU{channel: channel{lora.MHz_868_1,
4825
lora.Bandwidth_125_0,
4926
lora.SpreadingFactor9,
5027
lora.CodingRate4_7,
5128
EU868_DEFAULT_PREAMBLE_LEN,
52-
EU868_DEFAULT_TX_POWER_DBM},
53-
joinAcceptChannel: &ChannelEU{lora.MHz_868_1,
29+
EU868_DEFAULT_TX_POWER_DBM}},
30+
joinAcceptChannel: &ChannelEU{channel: channel{lora.MHz_868_1,
5431
lora.Bandwidth_125_0,
5532
lora.SpreadingFactor9,
5633
lora.CodingRate4_7,
5734
EU868_DEFAULT_PREAMBLE_LEN,
58-
EU868_DEFAULT_TX_POWER_DBM},
59-
uplinkChannel: &ChannelEU{lora.MHz_868_1,
35+
EU868_DEFAULT_TX_POWER_DBM}},
36+
uplinkChannel: &ChannelEU{channel: channel{lora.MHz_868_1,
6037
lora.Bandwidth_125_0,
6138
lora.SpreadingFactor9,
6239
lora.CodingRate4_7,
6340
EU868_DEFAULT_PREAMBLE_LEN,
64-
EU868_DEFAULT_TX_POWER_DBM},
65-
}
66-
}
67-
68-
func (r *RegionSettingsEU868) JoinRequestChannel() Channel {
69-
return r.joinRequestChannel
70-
}
71-
72-
func (r *RegionSettingsEU868) JoinAcceptChannel() Channel {
73-
return r.joinAcceptChannel
74-
}
75-
76-
func (r *RegionSettingsEU868) UplinkChannel() Channel {
77-
return r.uplinkChannel
41+
EU868_DEFAULT_TX_POWER_DBM}},
42+
}}
7843
}

lora/lorawan/region/regionset.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

lora/lorawan/region/settings.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package region
2+
3+
type Settings interface {
4+
JoinRequestChannel() Channel
5+
JoinAcceptChannel() Channel
6+
UplinkChannel() Channel
7+
}
8+
9+
type settings struct {
10+
joinRequestChannel Channel
11+
joinAcceptChannel Channel
12+
uplinkChannel Channel
13+
}
14+
15+
func (r *settings) JoinRequestChannel() Channel {
16+
return r.joinRequestChannel
17+
}
18+
19+
func (r *settings) JoinAcceptChannel() Channel {
20+
return r.joinAcceptChannel
21+
}
22+
23+
func (r *settings) UplinkChannel() Channel {
24+
return r.uplinkChannel
25+
}

lora/lorawan/region/us915.go

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,9 @@ const (
1010
)
1111

1212
type ChannelUS struct {
13-
frequency uint32
14-
bandwidth uint8
15-
spreadingFactor uint8
16-
codingRate uint8
17-
preambleLength uint16
18-
txPowerDBm int8
13+
channel
1914
}
2015

21-
// Getter functions
22-
func (c *ChannelUS) Frequency() uint32 { return c.frequency }
23-
func (c *ChannelUS) Bandwidth() uint8 { return c.bandwidth }
24-
func (c *ChannelUS) SpreadingFactor() uint8 { return c.spreadingFactor }
25-
func (c *ChannelUS) CodingRate() uint8 { return c.codingRate }
26-
func (c *ChannelUS) PreambleLength() uint16 { return c.preambleLength }
27-
func (c *ChannelUS) TxPowerDBm() int8 { return c.txPowerDBm }
28-
29-
// Set functions
30-
// TODO: validate input
31-
func (c *ChannelUS) SetFrequency(v uint32) { c.frequency = v }
32-
func (c *ChannelUS) SetBandwidth(v uint8) { c.bandwidth = v }
33-
func (c *ChannelUS) SetSpreadingFactor(v uint8) { c.spreadingFactor = v }
34-
func (c *ChannelUS) SetCodingRate(v uint8) { c.codingRate = v }
35-
func (c *ChannelUS) SetPreambleLength(v uint16) { c.preambleLength = v }
36-
func (c *ChannelUS) SetTxPowerDBm(v int8) { c.txPowerDBm = v }
37-
3816
func (c *ChannelUS) Next() bool {
3917
switch c.Bandwidth() {
4018
case lora.Bandwidth_125_0:
@@ -76,43 +54,29 @@ func stepFrequency500(freq uint32) (uint32, bool) {
7654
return f, true
7755
}
7856

79-
type RegionSettingsUS915 struct {
80-
joinRequestChannel *ChannelUS
81-
joinAcceptChannel *ChannelUS
82-
uplinkChannel *ChannelUS
57+
type SettingsUS915 struct {
58+
settings
8359
}
8460

85-
func US915() *RegionSettingsUS915 {
86-
return &RegionSettingsUS915{
87-
joinRequestChannel: &ChannelUS{lora.MHz_902_3,
61+
func US915() *SettingsUS915 {
62+
return &SettingsUS915{settings: settings{
63+
joinRequestChannel: &ChannelUS{channel: channel{lora.MHz_902_3,
8864
lora.Bandwidth_125_0,
8965
lora.SpreadingFactor10,
9066
lora.CodingRate4_5,
9167
US915_DEFAULT_PREAMBLE_LEN,
92-
US915_DEFAULT_TX_POWER_DBM},
93-
joinAcceptChannel: &ChannelUS{0,
68+
US915_DEFAULT_TX_POWER_DBM}},
69+
joinAcceptChannel: &ChannelUS{channel: channel{0,
9470
lora.Bandwidth_500_0,
9571
lora.SpreadingFactor9,
9672
lora.CodingRate4_5,
9773
US915_DEFAULT_PREAMBLE_LEN,
98-
US915_DEFAULT_TX_POWER_DBM},
99-
uplinkChannel: &ChannelUS{lora.Mhz_903_0,
74+
US915_DEFAULT_TX_POWER_DBM}},
75+
uplinkChannel: &ChannelUS{channel: channel{lora.Mhz_903_0,
10076
lora.Bandwidth_500_0,
10177
lora.SpreadingFactor9,
10278
lora.CodingRate4_5,
10379
US915_DEFAULT_PREAMBLE_LEN,
104-
US915_DEFAULT_TX_POWER_DBM},
105-
}
106-
}
107-
108-
func (r *RegionSettingsUS915) JoinRequestChannel() Channel {
109-
return r.joinRequestChannel
110-
}
111-
112-
func (r *RegionSettingsUS915) JoinAcceptChannel() Channel {
113-
return r.joinAcceptChannel
114-
}
115-
116-
func (r *RegionSettingsUS915) UplinkChannel() Channel {
117-
return r.uplinkChannel
80+
US915_DEFAULT_TX_POWER_DBM}},
81+
}}
11882
}

0 commit comments

Comments
 (0)