diff --git a/lora/lorawan/adaptor.go b/lora/lorawan/adaptor.go index 573328d..382a559 100644 --- a/lora/lorawan/adaptor.go +++ b/lora/lorawan/adaptor.go @@ -30,11 +30,11 @@ const ( var ( ActiveRadio lora.Radio Retries = 15 - regionSettings region.RegionSettings + regionSettings region.Settings ) // UseRegionSettings sets current Lorawan Regional parameters -func UseRegionSettings(rs region.RegionSettings) { +func UseRegionSettings(rs region.Settings) { regionSettings = rs } diff --git a/lora/lorawan/region/au915.go b/lora/lorawan/region/au915.go index cbfd25d..03c4149 100644 --- a/lora/lorawan/region/au915.go +++ b/lora/lorawan/region/au915.go @@ -8,75 +8,40 @@ const ( ) type ChannelAU struct { - frequency uint32 - bandwidth uint8 - spreadingFactor uint8 - codingRate uint8 - preambleLength uint16 - txPowerDBm int8 + channel } -// Getter functions -func (c *ChannelAU) Frequency() uint32 { return c.frequency } -func (c *ChannelAU) Bandwidth() uint8 { return c.bandwidth } -func (c *ChannelAU) SpreadingFactor() uint8 { return c.spreadingFactor } -func (c *ChannelAU) CodingRate() uint8 { return c.codingRate } -func (c *ChannelAU) PreambleLength() uint16 { return c.preambleLength } -func (c *ChannelAU) TxPowerDBm() int8 { return c.txPowerDBm } - -// Set functions -func (c *ChannelAU) SetFrequency(v uint32) { c.frequency = v } -func (c *ChannelAU) SetBandwidth(v uint8) { c.bandwidth = v } -func (c *ChannelAU) SetSpreadingFactor(v uint8) { c.spreadingFactor = v } -func (c *ChannelAU) SetCodingRate(v uint8) { c.codingRate = v } -func (c *ChannelAU) SetPreambleLength(v uint16) { c.preambleLength = v } -func (c *ChannelAU) SetTxPowerDBm(v int8) { c.txPowerDBm = v } - func (c *ChannelAU) Next() bool { return false } -type RegionSettingsAU915 struct { - joinRequestChannel *ChannelAU - joinAcceptChannel *ChannelAU - uplinkChannel *ChannelAU +type SettingsAU915 struct { + settings } -func AU915() *RegionSettingsAU915 { - return &RegionSettingsAU915{ - joinRequestChannel: &ChannelAU{lora.MHz_916_8, +func AU915() *SettingsAU915 { + return &SettingsAU915{settings: settings{ + joinRequestChannel: &ChannelAU{channel: channel{lora.MHz_916_8, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_5, AU915_DEFAULT_PREAMBLE_LEN, - AU915_DEFAULT_TX_POWER_DBM}, - joinAcceptChannel: &ChannelAU{lora.MHz_923_3, + AU915_DEFAULT_TX_POWER_DBM}}, + joinAcceptChannel: &ChannelAU{channel: channel{lora.MHz_923_3, lora.Bandwidth_500_0, lora.SpreadingFactor9, lora.CodingRate4_5, AU915_DEFAULT_PREAMBLE_LEN, - AU915_DEFAULT_TX_POWER_DBM}, - uplinkChannel: &ChannelAU{lora.MHz_916_8, + AU915_DEFAULT_TX_POWER_DBM}}, + uplinkChannel: &ChannelAU{channel: channel{lora.MHz_916_8, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_5, AU915_DEFAULT_PREAMBLE_LEN, - AU915_DEFAULT_TX_POWER_DBM}, - } + AU915_DEFAULT_TX_POWER_DBM}}, + }} } func Next(c *ChannelAU) bool { return false } - -func (r *RegionSettingsAU915) JoinRequestChannel() Channel { - return r.joinRequestChannel -} - -func (r *RegionSettingsAU915) JoinAcceptChannel() Channel { - return r.joinAcceptChannel -} - -func (r *RegionSettingsAU915) UplinkChannel() Channel { - return r.uplinkChannel -} diff --git a/lora/lorawan/region/channel.go b/lora/lorawan/region/channel.go new file mode 100644 index 0000000..b0e6526 --- /dev/null +++ b/lora/lorawan/region/channel.go @@ -0,0 +1,42 @@ +package region + +type Channel interface { + Next() bool + Frequency() uint32 + Bandwidth() uint8 + SpreadingFactor() uint8 + CodingRate() uint8 + PreambleLength() uint16 + TxPowerDBm() int8 + SetFrequency(v uint32) + SetBandwidth(v uint8) + SetSpreadingFactor(v uint8) + SetCodingRate(v uint8) + SetPreambleLength(v uint16) + SetTxPowerDBm(v int8) +} + +type channel struct { + frequency uint32 + bandwidth uint8 + spreadingFactor uint8 + codingRate uint8 + preambleLength uint16 + txPowerDBm int8 +} + +// Getter functions +func (c *channel) Frequency() uint32 { return c.frequency } +func (c *channel) Bandwidth() uint8 { return c.bandwidth } +func (c *channel) SpreadingFactor() uint8 { return c.spreadingFactor } +func (c *channel) CodingRate() uint8 { return c.codingRate } +func (c *channel) PreambleLength() uint16 { return c.preambleLength } +func (c *channel) TxPowerDBm() int8 { return c.txPowerDBm } + +// Set functions +func (c *channel) SetFrequency(v uint32) { c.frequency = v } +func (c *channel) SetBandwidth(v uint8) { c.bandwidth = v } +func (c *channel) SetSpreadingFactor(v uint8) { c.spreadingFactor = v } +func (c *channel) SetCodingRate(v uint8) { c.codingRate = v } +func (c *channel) SetPreambleLength(v uint16) { c.preambleLength = v } +func (c *channel) SetTxPowerDBm(v int8) { c.txPowerDBm = v } diff --git a/lora/lorawan/region/eu868.go b/lora/lorawan/region/eu868.go index d8325cc..7eed1f4 100644 --- a/lora/lorawan/region/eu868.go +++ b/lora/lorawan/region/eu868.go @@ -8,71 +8,36 @@ const ( ) type ChannelEU struct { - frequency uint32 - bandwidth uint8 - spreadingFactor uint8 - codingRate uint8 - preambleLength uint16 - txPowerDBm int8 + channel } -// Getter functions -func (c *ChannelEU) Frequency() uint32 { return c.frequency } -func (c *ChannelEU) Bandwidth() uint8 { return c.bandwidth } -func (c *ChannelEU) SpreadingFactor() uint8 { return c.spreadingFactor } -func (c *ChannelEU) CodingRate() uint8 { return c.codingRate } -func (c *ChannelEU) PreambleLength() uint16 { return c.preambleLength } -func (c *ChannelEU) TxPowerDBm() int8 { return c.txPowerDBm } - -// Set functions -func (c *ChannelEU) SetFrequency(v uint32) { c.frequency = v } -func (c *ChannelEU) SetBandwidth(v uint8) { c.bandwidth = v } -func (c *ChannelEU) SetSpreadingFactor(v uint8) { c.spreadingFactor = v } -func (c *ChannelEU) SetCodingRate(v uint8) { c.codingRate = v } -func (c *ChannelEU) SetPreambleLength(v uint16) { c.preambleLength = v } -func (c *ChannelEU) SetTxPowerDBm(v int8) { c.txPowerDBm = v } - func (c *ChannelEU) Next() bool { return false } -type RegionSettingsEU868 struct { - joinRequestChannel *ChannelEU - joinAcceptChannel *ChannelEU - uplinkChannel *ChannelEU +type SettingsEU868 struct { + settings } -func EU868() *RegionSettingsEU868 { - return &RegionSettingsEU868{ - joinRequestChannel: &ChannelEU{lora.MHz_868_1, +func EU868() *SettingsEU868 { + return &SettingsEU868{settings: settings{ + joinRequestChannel: &ChannelEU{channel: channel{lora.MHz_868_1, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_7, EU868_DEFAULT_PREAMBLE_LEN, - EU868_DEFAULT_TX_POWER_DBM}, - joinAcceptChannel: &ChannelEU{lora.MHz_868_1, + EU868_DEFAULT_TX_POWER_DBM}}, + joinAcceptChannel: &ChannelEU{channel: channel{lora.MHz_868_1, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_7, EU868_DEFAULT_PREAMBLE_LEN, - EU868_DEFAULT_TX_POWER_DBM}, - uplinkChannel: &ChannelEU{lora.MHz_868_1, + EU868_DEFAULT_TX_POWER_DBM}}, + uplinkChannel: &ChannelEU{channel: channel{lora.MHz_868_1, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_7, EU868_DEFAULT_PREAMBLE_LEN, - EU868_DEFAULT_TX_POWER_DBM}, - } -} - -func (r *RegionSettingsEU868) JoinRequestChannel() Channel { - return r.joinRequestChannel -} - -func (r *RegionSettingsEU868) JoinAcceptChannel() Channel { - return r.joinAcceptChannel -} - -func (r *RegionSettingsEU868) UplinkChannel() Channel { - return r.uplinkChannel + EU868_DEFAULT_TX_POWER_DBM}}, + }} } diff --git a/lora/lorawan/region/regionset.go b/lora/lorawan/region/regionset.go deleted file mode 100644 index 7983e69..0000000 --- a/lora/lorawan/region/regionset.go +++ /dev/null @@ -1,17 +0,0 @@ -package region - -type RegionSettings interface { - JoinRequestChannel() Channel - JoinAcceptChannel() Channel - UplinkChannel() Channel -} - -type Channel interface { - Next() bool - Frequency() uint32 - Bandwidth() uint8 - SpreadingFactor() uint8 - CodingRate() uint8 - PreambleLength() uint16 - TxPowerDBm() int8 -} diff --git a/lora/lorawan/region/settings.go b/lora/lorawan/region/settings.go new file mode 100644 index 0000000..e015c00 --- /dev/null +++ b/lora/lorawan/region/settings.go @@ -0,0 +1,25 @@ +package region + +type Settings interface { + JoinRequestChannel() Channel + JoinAcceptChannel() Channel + UplinkChannel() Channel +} + +type settings struct { + joinRequestChannel Channel + joinAcceptChannel Channel + uplinkChannel Channel +} + +func (r *settings) JoinRequestChannel() Channel { + return r.joinRequestChannel +} + +func (r *settings) JoinAcceptChannel() Channel { + return r.joinAcceptChannel +} + +func (r *settings) UplinkChannel() Channel { + return r.uplinkChannel +} diff --git a/lora/lorawan/region/us915.go b/lora/lorawan/region/us915.go index 68ec977..4149b5b 100644 --- a/lora/lorawan/region/us915.go +++ b/lora/lorawan/region/us915.go @@ -10,31 +10,9 @@ const ( ) type ChannelUS struct { - frequency uint32 - bandwidth uint8 - spreadingFactor uint8 - codingRate uint8 - preambleLength uint16 - txPowerDBm int8 + channel } -// Getter functions -func (c *ChannelUS) Frequency() uint32 { return c.frequency } -func (c *ChannelUS) Bandwidth() uint8 { return c.bandwidth } -func (c *ChannelUS) SpreadingFactor() uint8 { return c.spreadingFactor } -func (c *ChannelUS) CodingRate() uint8 { return c.codingRate } -func (c *ChannelUS) PreambleLength() uint16 { return c.preambleLength } -func (c *ChannelUS) TxPowerDBm() int8 { return c.txPowerDBm } - -// Set functions -// TODO: validate input -func (c *ChannelUS) SetFrequency(v uint32) { c.frequency = v } -func (c *ChannelUS) SetBandwidth(v uint8) { c.bandwidth = v } -func (c *ChannelUS) SetSpreadingFactor(v uint8) { c.spreadingFactor = v } -func (c *ChannelUS) SetCodingRate(v uint8) { c.codingRate = v } -func (c *ChannelUS) SetPreambleLength(v uint16) { c.preambleLength = v } -func (c *ChannelUS) SetTxPowerDBm(v int8) { c.txPowerDBm = v } - func (c *ChannelUS) Next() bool { switch c.Bandwidth() { case lora.Bandwidth_125_0: @@ -76,43 +54,29 @@ func stepFrequency500(freq uint32) (uint32, bool) { return f, true } -type RegionSettingsUS915 struct { - joinRequestChannel *ChannelUS - joinAcceptChannel *ChannelUS - uplinkChannel *ChannelUS +type SettingsUS915 struct { + settings } -func US915() *RegionSettingsUS915 { - return &RegionSettingsUS915{ - joinRequestChannel: &ChannelUS{lora.MHz_902_3, +func US915() *SettingsUS915 { + return &SettingsUS915{settings: settings{ + joinRequestChannel: &ChannelUS{channel: channel{lora.MHz_902_3, lora.Bandwidth_125_0, lora.SpreadingFactor10, lora.CodingRate4_5, US915_DEFAULT_PREAMBLE_LEN, - US915_DEFAULT_TX_POWER_DBM}, - joinAcceptChannel: &ChannelUS{0, + US915_DEFAULT_TX_POWER_DBM}}, + joinAcceptChannel: &ChannelUS{channel: channel{0, lora.Bandwidth_500_0, lora.SpreadingFactor9, lora.CodingRate4_5, US915_DEFAULT_PREAMBLE_LEN, - US915_DEFAULT_TX_POWER_DBM}, - uplinkChannel: &ChannelUS{lora.Mhz_903_0, + US915_DEFAULT_TX_POWER_DBM}}, + uplinkChannel: &ChannelUS{channel: channel{lora.Mhz_903_0, lora.Bandwidth_500_0, lora.SpreadingFactor9, lora.CodingRate4_5, US915_DEFAULT_PREAMBLE_LEN, - US915_DEFAULT_TX_POWER_DBM}, - } -} - -func (r *RegionSettingsUS915) JoinRequestChannel() Channel { - return r.joinRequestChannel -} - -func (r *RegionSettingsUS915) JoinAcceptChannel() Channel { - return r.joinAcceptChannel -} - -func (r *RegionSettingsUS915) UplinkChannel() Channel { - return r.uplinkChannel + US915_DEFAULT_TX_POWER_DBM}}, + }} }