-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add cli feature * Add detecting modem port * Add flags to detectCmd
- Loading branch information
Showing
7 changed files
with
366 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,5 @@ | |
|
||
# Go workspace file | ||
go.work | ||
|
||
RD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package atcom | ||
|
||
import ( | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
func getAvailablePorts() (availablePorts []map[string]string, err error) { | ||
cmd := exec.Command("bash", "-c", "/usr/bin/find /sys/bus/usb/devices/usb*/ -name dev") | ||
output, err := cmd.Output() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
outputStr := string(output) | ||
|
||
ports := make([]string, 0) | ||
|
||
for _, port := range strings.Split(outputStr, "\n") { | ||
if strings.HasSuffix(port, "/dev") { | ||
port = strings.TrimSuffix(port, "/dev") | ||
ports = append(ports, port) | ||
} | ||
} | ||
|
||
for _, port := range ports { | ||
cmd := exec.Command("bash", "-c", "udevadm info -q property --export -p "+port) | ||
output, err := cmd.Output() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
outputStr := string(output) | ||
|
||
deviceDetails := strings.Split(outputStr, "\n") | ||
|
||
portDetails := make(map[string]string) | ||
|
||
for _, line := range deviceDetails { | ||
switch { | ||
case strings.HasPrefix(line, "DEVNAME="): | ||
portDetails["port"] = strings.Trim(line[8:], "'") | ||
case strings.HasPrefix(line, "ID_VENDOR="): | ||
portDetails["vendor"] = strings.Trim(line[10:], "'") | ||
case strings.HasPrefix(line, "ID_VENDOR_ID="): | ||
portDetails["vendor_id"] = strings.Trim(line[13:], "'") | ||
case strings.HasPrefix(line, "ID_MODEL="): | ||
portDetails["model"] = strings.Trim(line[9:], "'") | ||
case strings.HasPrefix(line, "ID_MODEL_FROM_DATABASE="): | ||
portDetails["model_from_database"] = strings.Trim(line[23:], "'") | ||
case strings.HasPrefix(line, "ID_MODEL_ID="): | ||
portDetails["product_id"] = strings.Trim(line[12:], "'") | ||
case strings.HasPrefix(line, "ID_USB_INTERFACE_NUM="): | ||
portDetails["interface"] = "if" + strings.Trim(line[21:], "'") | ||
case strings.HasPrefix(line, "ID_USB_VENDOR_ID="): | ||
portDetails["ID_USB_VENDOR_ID"] = strings.Trim(line[17:], "'") | ||
case strings.HasPrefix(line, "ID_USB_MODEL_ID="): | ||
portDetails["ID_USB_MODEL_ID"] = strings.Trim(line[16:], "'") | ||
} | ||
} | ||
|
||
if !strings.Contains(portDetails["port"], "bus") { | ||
availablePorts = append(availablePorts, portDetails) | ||
} | ||
} | ||
return availablePorts, nil | ||
} | ||
|
||
func findModems() (SupportedModem, error) { | ||
cmd := exec.Command("lsusb") | ||
output, err := cmd.Output() | ||
|
||
if err != nil { | ||
return SupportedModem{}, err | ||
} | ||
|
||
outputStr := string(output) | ||
|
||
for _, modem := range supportedModems { | ||
if outputStr == "" { | ||
return modem, nil | ||
} | ||
|
||
for _, line := range strings.Split(outputStr, "\n") { | ||
if strings.Contains(line, modem.vid) && strings.Contains(line, modem.pid) { | ||
return modem, nil | ||
} | ||
} | ||
} | ||
return SupportedModem{}, nil | ||
} | ||
|
||
func DecidePort() (map[string]string, error) { | ||
modem, err := findModems() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ports, err := getAvailablePorts() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, port := range ports { | ||
if port["vendor_id"] == modem.vid && | ||
port["product_id"] == modem.pid && | ||
port["interface"] == modem.ifs { | ||
|
||
detectedModem := map[string]string{ | ||
"port": port["port"], | ||
"vendor_id": port["vendor_id"], | ||
"product_id": port["product_id"], | ||
} | ||
|
||
return detectedModem, nil | ||
} | ||
} | ||
|
||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= | ||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= | ||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= | ||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= | ||
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= | ||
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUYb9Fbt8uiOSooupjTq10vpvnU= | ||
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= | ||
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= | ||
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package atcom | ||
|
||
type SupportedModem struct { | ||
vid string | ||
pid string | ||
vendor string | ||
product string | ||
ifs string | ||
} | ||
|
||
var supportedModems = []SupportedModem{ | ||
// Quectel | ||
{"2c7c", "0125", "Quectel", "EC25", "if02"}, | ||
{"2c7c", "0121", "Quectel", "EC21", "if02"}, | ||
{"2c7c", "0296", "Quectel", "BG96", "if02"}, | ||
{"2c7c", "0700", "Quectel", "BG95", "if02"}, | ||
{"2c7c", "0306", "Quectel", "EP06", "if02"}, | ||
{"2c7c", "0800", "Quectel", "RM5XXQ", "if02"}, | ||
// Telit | ||
{"1bc7", "1201", "Telit", "LE910Cx RMNET", "if04"}, | ||
{"1bc7", "1203", "Telit", "LE910Cx RNDIS", "if05"}, | ||
{"1bc7", "1204", "Telit", "LE910Cx MBIM", "if05"}, | ||
{"1bc7", "1206", "Telit", "LE910Cx ECM", "if05"}, | ||
{"1bc7", "1031", "Telit", "LE910Cx ThreadX RMNET", "if02"}, | ||
{"1bc7", "1033", "Telit", "LE910Cx ThreadX ECM", "if02"}, | ||
{"1bc7", "1034", "Telit", "LE910Cx ThreadX RMNET", "if00"}, | ||
{"1bc7", "1035", "Telit", "LE910Cx ThreadX ECM", "if00"}, | ||
{"1bc7", "1036", "Telit", "LE910Cx ThreadX OPTION ONLY", "if00"}, | ||
{"1bc7", "1101", "Telit", "ME910C1", "if01"}, | ||
{"1bc7", "1102", "Telit", "ME910C1", "if01"}, | ||
{"1bc7", "1052", "Telit", "FN980 RNDIS", "if05"}, | ||
{"1bc7", "1050", "Telit", "FN980 RMNET", "if04"}, | ||
{"1bc7", "1051", "Telit", "FN980 MBIM", "if05"}, | ||
{"1bc7", "1053", "Telit", "FN980 ECM", "if05"}, | ||
// Thales | ||
{"1e2d", "0069", "Thales/Cinterion", "PLSx3", "if04"}, | ||
{"1e2d", "006f", "Thales/Cinterion", "PLSx3", "if04"}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
atcom "github.com/sixfab/atcomv2" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// versionCmd represents the version command | ||
var versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Print the version number", | ||
Long: `Print the version number`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("Version:", atcom.Version) | ||
}, | ||
} | ||
|
||
// detectCmd represents the detect command | ||
var detectCmd = &cobra.Command{ | ||
Use: "detect", | ||
Short: "Detect the modem port", | ||
Long: `Detect the modem port`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
allFlag := cmd.Flag("all").Value.String() | ||
vidFlag := cmd.Flag("vid").Value.String() | ||
pidFlag := cmd.Flag("pid").Value.String() | ||
portFlag := cmd.Flag("port").Value.String() | ||
|
||
modem, err := atcom.DecidePort() | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
if allFlag == "true" { | ||
for key, value := range modem { | ||
fmt.Println(key + ":" + value) | ||
} | ||
} | ||
|
||
if vidFlag == "true" { | ||
fmt.Println(modem["vendor_id"]) | ||
} | ||
|
||
if pidFlag == "true" { | ||
fmt.Println(modem["product_id"]) | ||
} | ||
|
||
if portFlag == "true" { | ||
fmt.Println(modem["port"]) | ||
} | ||
}, | ||
} | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "atcomv2cli [AT] [flags]", | ||
Short: "AT Command CLI", | ||
Long: `AT Command CLI for communicating cellular modems with AT commands.`, | ||
Args: cobra.MinimumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
command := args[0] | ||
port := cmd.Flag("port").Value.String() | ||
baud := cmd.Flag("baud").Value.String() | ||
desired := cmd.Flag("desired").Value.String() | ||
fault := cmd.Flag("fault").Value.String() | ||
timeout := cmd.Flag("timeout").Value.String() | ||
lineend := cmd.Flag("lineend").Value.String() | ||
verbose := cmd.Flag("verbose").Value.String() | ||
|
||
// convert parameters to suitable format with library | ||
baudInt, _ := strconv.Atoi(baud) | ||
timeoutInt, _ := strconv.Atoi(timeout) | ||
lineendBool, _ := strconv.ParseBool(lineend) | ||
|
||
desiredSlice := []string{} | ||
faultSlice := []string{} | ||
|
||
if desired != "" { | ||
words := strings.Split(desired, "/") | ||
desiredSlice = append(desiredSlice, words...) | ||
} else { | ||
desiredSlice = nil | ||
} | ||
|
||
if fault != "" { | ||
words := strings.Split(fault, "/") | ||
faultSlice = append(faultSlice, words...) | ||
} else { | ||
faultSlice = nil | ||
} | ||
|
||
// prepare command arguments | ||
comArgs := map[string]interface{}{ | ||
"port": port, | ||
"baud": baudInt, | ||
"desired": desiredSlice, | ||
"fault": faultSlice, | ||
"timeout": timeoutInt, | ||
"lineEnd": lineendBool, | ||
} | ||
|
||
// if verbose mode is true, print parameters | ||
if verbose == "true" { | ||
fmt.Println("--------------------------------------") | ||
fmt.Println("Command: ", command) | ||
fmt.Println("Port: ", port) | ||
fmt.Println("Baud: ", baud) | ||
fmt.Println("Desired: ", desiredSlice) | ||
fmt.Println("Fault: ", faultSlice) | ||
fmt.Println("Timeout: ", timeout) | ||
fmt.Println("Verbose: ", verbose) | ||
fmt.Println("--------------------------------------") | ||
fmt.Println("") | ||
} | ||
|
||
response, err := atcom.SendAT(command, comArgs) | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
for _, res := range response { | ||
fmt.Println(res) | ||
} | ||
}, | ||
} | ||
|
||
// 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() { | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
// Here you will define your flags and configuration settings. | ||
// Cobra supports persistent flags, which, if defined here, | ||
// will be global for your application. | ||
|
||
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.atcomv2cli.yaml)") | ||
|
||
// Cobra also supports local flags, which will only run | ||
// when this action is called directly. | ||
rootCmd.Flags().StringP("port", "p", "/dev/ttyUSB2", "port name") | ||
rootCmd.Flags().IntP("baud", "b", 115200, "baud rate") | ||
rootCmd.Flags().StringP("desired", "d", "", "desired responses - separate your multiple words with /") | ||
rootCmd.Flags().StringP("fault", "f", "", "fault responses - separate your multiple words with /") | ||
rootCmd.Flags().IntP("timeout", "t", 5, "timeout duration in seconds") | ||
rootCmd.Flags().BoolP("lineend", "l", true, "line end") | ||
rootCmd.Flags().BoolP("verbose", "v", false, "verbose mode") | ||
rootCmd.Flags().StringP("version", "V", "", "version") | ||
|
||
rootCmd.AddCommand(versionCmd) | ||
rootCmd.AddCommand(detectCmd) | ||
|
||
detectCmd.Flags().BoolP("all", "a", false, "all modem attributes") | ||
detectCmd.Flags().BoolP("vid", "v", false, "vendor id") | ||
detectCmd.Flags().BoolP("pid", "i", false, "product id") | ||
detectCmd.Flags().BoolP("port", "p", false, "serial port") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
Created by: Yasin Kaya (selengalp), [email protected], 2023 | ||
Copyright © 2023 Sixfab Inc. | ||
*/ | ||
package main | ||
|
||
import "github.com/sixfab/atcomv2/pkg/cli/cmd" | ||
|
||
func main() { | ||
cmd.Execute() | ||
} |