diff --git a/cli_client/cmd_led.go b/cli_client/cmd_led.go index edfb9b9..9da381e 100644 --- a/cli_client/cmd_led.go +++ b/cli_client/cmd_led.go @@ -19,6 +19,13 @@ var ledCmd = &cobra.Command{ var ledGetCmd = &cobra.Command{ Use: "get", Short: "Get LED blink count", + Run: func(cmd *cobra.Command, args []string) { + if ls, err := ClientGetLED(StrRemoteHost, StrRemotePort); err == nil { + fmt.Printf("LED blink count %v\n", ls.BlinkCount) + } else { + log.Println(err) + } + }, } var ledSetCmd = &cobra.Command{ @@ -44,14 +51,5 @@ func init() { ledCmd.AddCommand(ledGetCmd) ledCmd.AddCommand(ledSetCmd) - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // usbCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // usbCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") ledSetCmd.Flags().Uint32Var(&blink_count,"blink", 0,"Set blink count (0: Off, 1..254: blink n times, >254: On)") } diff --git a/cli_client/cmd_root.go b/cli_client/cmd_root.go index d6bbd31..b8c3d16 100644 --- a/cli_client/cmd_root.go +++ b/cli_client/cmd_root.go @@ -19,13 +19,8 @@ var rootCmd = &cobra.Command{ Long: `The cli_client tool could be used to configure P4wnP1 from the command line. The tool relies on RPC so it could be used remotely.`, - // Uncomment the following line if your bare application - // has an action associated with it: - // Run: func(cmd *cobra.Command, args []string) { }, } -// Execute adds all child commands to the root command and sets flags appropriately. -// This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { if err := rootCmd.Execute(); err != nil { fmt.Println(err) diff --git a/cli_client/cmd_usb.go b/cli_client/cmd_usb.go new file mode 100644 index 0000000..6b68362 --- /dev/null +++ b/cli_client/cmd_usb.go @@ -0,0 +1,144 @@ +package cli_client + +import ( + "fmt" + "github.com/spf13/cobra" + "log" + +) + +//Empty settings used to store cobra flags +var ( + //tmpGadgetSettings = pb.GadgetSettings{CdcEcmSettings:&pb.GadgetSettingsEthernet{},RndisSettings:&pb.GadgetSettingsEthernet{}} + tmpUseHIDKeyboard uint8 = 0 + tmpUseHIDMouse uint8 = 0 + tmpUseHIDRaw uint8 = 0 + tmpUseRNDIS uint8 = 0 + tmpUseECM uint8 = 0 + tmpUseSerial uint8 = 0 + tmpUseUMS uint8 = 0 +) + + +// usbCmd represents the usb command +var usbCmd = &cobra.Command{ + Use: "USB", + Short: "Set, get or deploy USB Gadget settings", +} + +var usbGetCmd = &cobra.Command{ + Use: "get", + Short: "Get USB Gadget settings", + Long: ``, + Run: cobraUsbGet, +} + +var usbSetCmd = &cobra.Command{ + Use: "set", + Short: "set USB Gadget settings", + Long: ``, + Run: cobraUsbSet, +} + +func cobraUsbSet(cmd *cobra.Command, args []string) { + gs, err := ClientGetGadgetSettings(StrRemoteHost, StrRemotePort) + if err != nil { + log.Println(err) + return + } + + fmt.Printf("USB Gadget Settings retreived: %+v\n", gs) + + if (cmd.Flags().Lookup("rndis").Changed) { + if tmpUseRNDIS == 0 { + fmt.Println("Disabeling RNDIS") + gs.Use_RNDIS = false + } else { + fmt.Println("Enabeling RNDIS") + gs.Use_RNDIS = true + } + } + + if (cmd.Flags().Lookup("cdc-ecm").Changed) { + if tmpUseRNDIS == 0 { + fmt.Println("Disabeling CDC ECM") + gs.Use_CDC_ECM = false + } else { + fmt.Println("Enabeling RNDIS") + gs.Use_CDC_ECM = true + } + } + + if (cmd.Flags().Lookup("serial").Changed) { + if tmpUseRNDIS == 0 { + fmt.Println("Disabeling Serial") + gs.Use_SERIAL = false + } else { + fmt.Println("Enabeling Serial") + gs.Use_SERIAL = true + } + } + + if (cmd.Flags().Lookup("hid-keyboard").Changed) { + if tmpUseRNDIS == 0 { + fmt.Println("Disabeling HID keyboard") + gs.Use_HID_KEYBOARD = false + } else { + fmt.Println("Enabeling HID keyboard") + gs.Use_HID_KEYBOARD = true + } + } + + + //ToDo: Implement the rest (HID, UMS etc.) + + //Try to set the change config + err = ClientSetGadgetSettings(StrRemoteHost, StrRemotePort, *gs) + if err != nil { + log.Printf("Error setting new gadget settings: %v\n", err) + } + + gs, err = ClientGetGadgetSettings(StrRemoteHost, StrRemotePort) + if err != nil { + log.Println(err) + return + } + + fmt.Printf("USB Gadget Settings set: %+v\n", gs) + return +} + +func cobraUsbGet(cmd *cobra.Command, args []string) { + if gs, err := ClientGetGadgetSettings(StrRemoteHost, StrRemotePort); err == nil { + fmt.Printf("USB Gadget Settings: %+v\n", gs) + } else { + log.Println(err) + } +} + +func init() { + rootCmd.AddCommand(usbCmd) + usbCmd.AddCommand(usbGetCmd) + usbCmd.AddCommand(usbSetCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // usbCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // usbCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + + + usbSetCmd.Flags().Uint8VarP(&tmpUseRNDIS, "rndis", "n",0,"Use the RNDIS gadget function (0: disable, 1..n: enable)") + usbSetCmd.Flags().Uint8VarP(&tmpUseECM, "cdc-ecm", "e",0,"Use the CDC ECM gadget function (0: disable, 1..n: enable)") + usbSetCmd.Flags().Uint8VarP(&tmpUseSerial, "serial", "s",0,"Use the SERIAL gadget function (0: disable, 1..n: enable)") + + usbSetCmd.Flags().Uint8VarP(&tmpUseHIDKeyboard, "hid-keyboard", "k",0,"Use the HID KEYBOARD gadget function (0: disable, 1..n: enable)") + usbSetCmd.Flags().Uint8VarP(&tmpUseHIDMouse, "hid-mouse", "m",0,"Use the HID MOUSE gadget function (0: disable, 1..n: enable)") + usbSetCmd.Flags().Uint8VarP(&tmpUseHIDRaw, "hid-raw", "r",0,"Use the HID RAW gadget function (0: disable, 1..n: enable)") + + usbSetCmd.Flags().Uint8VarP(&tmpUseUMS, "ums", "u",0,"Use the USB MAss Storage gadget function (0: disable, 1..n: enable)") +} diff --git a/cli_client/rpc.go b/cli_client/rpc.go index 854dc1d..359d788 100644 --- a/cli_client/rpc.go +++ b/cli_client/rpc.go @@ -17,10 +17,10 @@ func ClientConnectServer(rpcHost string, rpcPort string) ( err error) { // Set up a connection to the server. address := rpcHost + ":" + rpcPort - log.Printf("Connecting %s ...", address) + //log.Printf("Connecting %s ...", address) connection, err = grpc.Dial(address, grpc.WithInsecure()) if err != nil { - log.Fatalf("did not connect: %v", err) + log.Fatalf("Could not connect to P4wnP1 RPC server: %v", err) } //defer conn.Close() @@ -34,6 +34,52 @@ func ClientConnectServer(rpcHost string, rpcPort string) ( return } +func ClientGetLED(host string, port string) (ls *pb.LEDSettings, err error) { + conn, client, ctx, cancel, err := ClientConnectServer(host, port) + if err != nil { + return + } + + ls, err = client.GetLEDSettings(ctx, &pb.Empty{}) + if err != nil { + log.Printf("Error getting LED blink count: %v", err) + } + + ClientDisconnectServer(cancel, conn) + return +} + +func ClientGetGadgetSettings(host string, port string) (gs *pb.GadgetSettings, err error) { + conn, client, ctx, cancel, err := ClientConnectServer(host, port) + if err != nil { + return + } + + gs, err = client.GetGadgetSettings(ctx, &pb.Empty{}) + if err != nil { + log.Printf("Error getting USB Gadget Settings count: %+v", err) + } + + ClientDisconnectServer(cancel, conn) + return +} + + +func ClientSetGadgetSettings(host string, port string, gs pb.GadgetSettings) (err error) { + conn, client, ctx, cancel, err := ClientConnectServer(host, port) + if err != nil { + return + } + + _, err = client.SetGadgetSettings(ctx, &gs) + if err != nil { + log.Printf("Error setting GadgetSettings %d: %+v", gs, err) + } + + ClientDisconnectServer(cancel, conn) + return +} + func ClientSetLED(host string, port string, ls pb.LEDSettings) (err error) { conn, client, ctx, cancel, err := ClientConnectServer(host, port) if err != nil { diff --git a/cli_client/usb.go b/cli_client/usb.go deleted file mode 100644 index 01c1154..0000000 --- a/cli_client/usb.go +++ /dev/null @@ -1,41 +0,0 @@ -package cli_client - -import ( - "fmt" - - "github.com/spf13/cobra" -) - -// usbCmd represents the usb command -var usbCmd = &cobra.Command{ - Use: "usb", - Short: "Set or get USB Gadget settings", - Long: ``, - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("usb called") - }, -} - -var usbGetCmd = &cobra.Command{ - Use: "get", - Short: "Get USB Gadget settings", - Long: ``, - Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("usb get called %v", args) - }, -} - -func init() { - rootCmd.AddCommand(usbCmd) - usbCmd.AddCommand(usbGetCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // usbCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // usbCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") -} diff --git a/service/rpc.go b/service/rpc.go index d9d014c..d9e6454 100644 --- a/service/rpc.go +++ b/service/rpc.go @@ -57,7 +57,9 @@ func (s *server) SetGadgetSettings(ctx context.Context, gs *pb.GadgetSettings) ( } func (s *server) GetLEDSettings(context.Context, *pb.Empty) (res *pb.LEDSettings, err error) { - return GetLed() + res, err = GetLed() + log.Printf("GetLEDSettings, result: %+v", res) + return } func (s *server) SetLEDSettings(ctx context.Context, ls *pb.LEDSettings) (*pb.Empty, error) {