<commit_before>// Copyright © 2016 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.

package cmd

import (
	"encoding/base64"
	"net"
	"strconv"
	"strings"
	"time"

	"github.com/TheThingsNetwork/ttn/semtech"
	"github.com/TheThingsNetwork/ttn/ttnctl/util"
	"github.com/TheThingsNetwork/ttn/utils/pointer"
	"github.com/brocaar/lorawan"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

// uplinkCmd represents the `uplink` command
var uplinkCmd = &cobra.Command{
	Use:   "uplink [DevAddr] [NwkSKey] [AppSKey] [Payload] [FCnt]",
	Short: "Send an uplink message to the network",
	Long:  `Send an uplink message to the network`,
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) < 5 {
			ctx.Fatalf("Insufficient arguments")
		}

		// Parse parameters
		devAddrRaw, err := util.Parse32(args[0])
		if err != nil {
			ctx.Fatalf("Invalid DevAddr: %s", err)
		}
		var devAddr lorawan.DevAddr
		copy(devAddr[:], devAddrRaw)

		nwkSKeyRaw, err := util.Parse128(args[1])
		if err != nil {
			ctx.Fatalf("Invalid NwkSKey: %s", err)
		}
		var nwkSKey lorawan.AES128Key
		copy(nwkSKey[:], nwkSKeyRaw[:])

		appSKeyRaw, err := util.Parse128(args[2])
		if err != nil {
			ctx.Fatalf("Invalid appSKey: %s", err)
		}
		var appSKey lorawan.AES128Key
		copy(appSKey[:], appSKeyRaw[:])

		fcnt, err := strconv.ParseInt(args[4], 10, 64)
		if err != nil {
			ctx.Fatalf("Invalid FCnt: %s", err)
		}

		// Lorawan Payload
		macPayload := lorawan.NewMACPayload(true)
		macPayload.FHDR = lorawan.FHDR{
			DevAddr: devAddr,
			FCnt:    uint32(fcnt),
		}
		macPayload.FPort = 1
		macPayload.FRMPayload = []lorawan.Payload{&lorawan.DataPayload{Bytes: []byte(args[4])}}
		if err := macPayload.EncryptFRMPayload(appSKey); err != nil {
			ctx.Fatalf("Unable to encrypt frame payload: %s", err)
		}
		phyPayload := lorawan.NewPHYPayload(true)
		phyPayload.MHDR = lorawan.MHDR{
			MType: lorawan.UnconfirmedDataUp,
			Major: lorawan.LoRaWANR1,
		}
		phyPayload.MACPayload = macPayload
		if err := phyPayload.SetMIC(nwkSKey); err != nil {<commit_msg>[fix/ttnctl] Use of right argument index in ttnctl<commit_after>// Copyright © 2016 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.

package cmd

import (
	"encoding/base64"
	"net"
	"strconv"
	"strings"
	"time"

	"github.com/TheThingsNetwork/ttn/semtech"
	"github.com/TheThingsNetwork/ttn/ttnctl/util"
	"github.com/TheThingsNetwork/ttn/utils/pointer"
	"github.com/brocaar/lorawan"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

// uplinkCmd represents the `uplink` command
var uplinkCmd = &cobra.Command{
	Use:   "uplink [DevAddr] [NwkSKey] [AppSKey] [Payload] [FCnt]",
	Short: "Send an uplink message to the network",
	Long:  `Send an uplink message to the network`,
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) < 5 {
			ctx.Fatalf("Insufficient arguments")
		}

		// Parse parameters
		devAddrRaw, err := util.Parse32(args[0])
		if err != nil {
			ctx.Fatalf("Invalid DevAddr: %s", err)
		}
		var devAddr lorawan.DevAddr
		copy(devAddr[:], devAddrRaw)

		nwkSKeyRaw, err := util.Parse128(args[1])
		if err != nil {
			ctx.Fatalf("Invalid NwkSKey: %s", err)
		}
		var nwkSKey lorawan.AES128Key
		copy(nwkSKey[:], nwkSKeyRaw[:])

		appSKeyRaw, err := util.Parse128(args[2])
		if err != nil {
			ctx.Fatalf("Invalid appSKey: %s", err)
		}
		var appSKey lorawan.AES128Key
		copy(appSKey[:], appSKeyRaw[:])

		fcnt, err := strconv.ParseInt(args[4], 10, 64)
		if err != nil {
			ctx.Fatalf("Invalid FCnt: %s", err)
		}

		// Lorawan Payload
		macPayload := lorawan.NewMACPayload(true)
		macPayload.FHDR = lorawan.FHDR{
			DevAddr: devAddr,
			FCnt:    uint32(fcnt),
		}
		macPayload.FPort = 1
		macPayload.FRMPayload = []lorawan.Payload{&lorawan.DataPayload{Bytes: []byte(args[3])}}
		if err := macPayload.EncryptFRMPayload(appSKey); err != nil {
			ctx.Fatalf("Unable to encrypt frame payload: %s", err)
		}
		phyPayload := lorawan.NewPHYPayload(true)
		phyPayload.MHDR = lorawan.MHDR{
			MType: lorawan.UnconfirmedDataUp,
			Major: lorawan.LoRaWANR1,
		}
		phyPayload.MACPayload = macPayload
		if err := phyPayload.SetMIC(nwkSKey); err != nil {
{'fraction_non_alphanumeric': 0.1423848878394333, 'fraction_numerical': 0.0179456906729634, 'mean_word_length': 4.02491103202847, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7619506', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1666, 'n_words': 371}