func (r *router) HandleActivation(gatewayID string, activation *pb.DeviceActivationRequest) (res *pb.DeviceActivationResponse, err error) { ctx := r.Ctx.WithFields(log.Fields{ "GatewayID": gatewayID, "AppEUI": *activation.AppEui, "DevEUI": *activation.DevEui, }) start := time.Now() defer func() { if err != nil { ctx.WithError(err).Warn("Could not handle activation") } else { ctx.WithField("Duration", time.Now().Sub(start)).Info("Handled activation") } }() r.status.activations.Mark(1) gateway := r.getGateway(gatewayID) gateway.LastSeen = time.Now() uplink := &pb.UplinkMessage{ Payload: activation.Payload, ProtocolMetadata: activation.ProtocolMetadata, GatewayMetadata: activation.GatewayMetadata, } if err = gateway.HandleUplink(uplink); err != nil { return nil, err } if !gateway.Schedule.IsActive() { return nil, errors.NewErrInternal(fmt.Sprintf("Gateway %s not available for downlink", gatewayID)) } downlinkOptions := r.buildDownlinkOptions(uplink, true, gateway) // Find Broker brokers, err := r.Discovery.GetAll("broker") if err != nil { return nil, err } // Prepare request request := &pb_broker.DeviceActivationRequest{ Payload: activation.Payload, DevEui: activation.DevEui, AppEui: activation.AppEui, ProtocolMetadata: activation.ProtocolMetadata, GatewayMetadata: activation.GatewayMetadata, ActivationMetadata: &pb_protocol.ActivationMetadata{ Protocol: &pb_protocol.ActivationMetadata_Lorawan{ Lorawan: &pb_lorawan.ActivationMetadata{ AppEui: activation.AppEui, DevEui: activation.DevEui, }, }, }, DownlinkOptions: downlinkOptions, } // Prepare LoRaWAN activation status, err := gateway.Status.Get() if err != nil { return nil, err } region := status.Region if region == "" { region = band.Guess(uplink.GatewayMetadata.Frequency) } band, err := band.Get(region) if err != nil { return nil, err } lorawan := request.ActivationMetadata.GetLorawan() lorawan.Rx1DrOffset = 0 lorawan.Rx2Dr = uint32(band.RX2DataRate) lorawan.RxDelay = uint32(band.ReceiveDelay1.Seconds()) if band.CFList != nil { lorawan.CfList = new(pb_lorawan.CFList) for _, freq := range band.CFList { lorawan.CfList.Freq = append(lorawan.CfList.Freq, freq) } } ctx = ctx.WithField("NumBrokers", len(brokers)) // Forward to all brokers and collect responses var wg sync.WaitGroup responses := make(chan *pb_broker.DeviceActivationResponse, len(brokers)) for _, broker := range brokers { broker, err := r.getBroker(broker) if err != nil { continue } // Do async request wg.Add(1) go func() { res, err := broker.client.Activate(r.Component.GetContext(""), request) if err == nil && res != nil { responses <- res } wg.Done() }() } // Make sure to close channel when all requests are done go func() { wg.Wait() close(responses) }() var gotFirst bool for res := range responses { if gotFirst { ctx.Warn("Duplicate Activation Response") } else { gotFirst = true downlink := &pb_broker.DownlinkMessage{ Payload: res.Payload, Message: res.Message, DownlinkOption: res.DownlinkOption, } err := r.HandleDownlink(downlink) if err != nil { ctx.Warn("Could not send downlink for Activation") gotFirst = false // try again } } } // Activation not accepted by any broker if !gotFirst { ctx.Debug("Activation not accepted at this gateway") return nil, errors.New("Activation not accepted at this Gateway") } // Activation accepted by (at least one) broker ctx.Debug("Activation accepted") return &pb.DeviceActivationResponse{}, nil }
func (r *router) buildDownlinkOptions(uplink *pb.UplinkMessage, isActivation bool, gateway *gateway.Gateway) (downlinkOptions []*pb_broker.DownlinkOption) { var options []*pb_broker.DownlinkOption gatewayStatus, _ := gateway.Status.Get() // This just returns empty if non-existing lorawanMetadata := uplink.ProtocolMetadata.GetLorawan() if lorawanMetadata == nil { return // We can't handle any other protocols than LoRaWAN yet } region := gatewayStatus.Region if region == "" { region = band.Guess(uplink.GatewayMetadata.Frequency) } band, err := band.Get(region) if err != nil { return // We can't handle this region } if region == "EU_863_870" && isActivation { band.RX2DataRate = 0 } dataRate, err := lorawanMetadata.GetDataRate() if err != nil { return } // Configuration for RX2 buildRX2 := func() (*pb_broker.DownlinkOption, error) { option := r.buildDownlinkOption(gateway.ID, band) if region == "EU_863_870" { option.GatewayConfig.Power = 27 // The EU RX2 frequency allows up to 27dBm } if isActivation { option.GatewayConfig.Timestamp = uplink.GatewayMetadata.Timestamp + uint32(band.JoinAcceptDelay2/1000) } else { option.GatewayConfig.Timestamp = uplink.GatewayMetadata.Timestamp + uint32(band.ReceiveDelay2/1000) } option.ProtocolConfig.GetLorawan().CodingRate = lorawanMetadata.CodingRate return option, nil } if option, err := buildRX2(); err == nil { options = append(options, option) } // Configuration for RX1 buildRX1 := func() (*pb_broker.DownlinkOption, error) { option := r.buildDownlinkOption(gateway.ID, band) if isActivation { option.GatewayConfig.Timestamp = uplink.GatewayMetadata.Timestamp + uint32(band.JoinAcceptDelay1/1000) } else { option.GatewayConfig.Timestamp = uplink.GatewayMetadata.Timestamp + uint32(band.ReceiveDelay1/1000) } option.ProtocolConfig.GetLorawan().CodingRate = lorawanMetadata.CodingRate freq, err := band.GetRX1Frequency(int(uplink.GatewayMetadata.Frequency)) if err != nil { return nil, err } option.GatewayConfig.Frequency = uint64(freq) upDR, err := band.GetDataRate(dataRate) if err != nil { return nil, err } downDR, err := band.GetRX1DataRate(upDR, 0) if err != nil { return nil, err } if err := option.ProtocolConfig.GetLorawan().SetDataRate(band.DataRates[downDR]); err != nil { return nil, err } option.GatewayConfig.FrequencyDeviation = uint32(option.ProtocolConfig.GetLorawan().BitRate / 2) return option, nil } if option, err := buildRX1(); err == nil { options = append(options, option) } computeDownlinkScores(gateway, uplink, options) for _, option := range options { // Add router ID to downlink option if r.Component != nil && r.Component.Identity != nil { option.Identifier = fmt.Sprintf("%s:%s", r.Component.Identity.Id, option.Identifier) } // Filter all illegal options if option.Score < 1000 { downlinkOptions = append(downlinkOptions, option) } } return }