diff --git a/.gitignore b/.gitignore index 9ed3b07..3fc793c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.test +intelhex/intelhex \ No newline at end of file diff --git a/intelhex/README.md b/intelhex/README.md new file mode 100644 index 0000000..eebd80a --- /dev/null +++ b/intelhex/README.md @@ -0,0 +1,27 @@ +# IntelHex + +Decode Intel Hex file, encode data in Intel Hex format. Hex files are use to programm eeprom or microcontroller + +## Usage + + intelhex [command] + +Available Commands: + + * read Read data from hex file + * write Write data from binary file + + +### Read + intelhex read "path" [flags] + +Flags: + + --binary display binary + +### Write + intelhex write "path" [flags] + +Flags: + + --immediate immediate string in encoded diff --git a/intelhex/main.go b/intelhex/main.go new file mode 100644 index 0000000..eda0481 --- /dev/null +++ b/intelhex/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + + "github.com/spf13/cobra" + "github.com/spf13/viper" + + "github.com/mash/go-intelhex/intelhex/reader" + "github.com/mash/go-intelhex/intelhex/writer" +) + +/* Main Command to parse + command line */ +var mainCommand = &cobra.Command{ + Use: "intelhex", + Short: "Intel hex parser / encoder", + Long: "Parse or encode data to intel HEX", + Run: func(cmd *cobra.Command, args []string) { + viper.AutomaticEnv() + // Application statup here + err := mainApp() + if err != nil { + fmt.Println(err) + } + }, +} + +/** + * The Main application really starts here + */ +func mainApp() (err error) { + + return nil +} + +func main() { + mainCommand.Execute() +} + +func init() { + mainCommand.AddCommand(reader.MainCmd) + mainCommand.AddCommand(writer.MainCmd) +} diff --git a/intelhex/reader/reader.go b/intelhex/reader/reader.go new file mode 100644 index 0000000..abf23b9 --- /dev/null +++ b/intelhex/reader/reader.go @@ -0,0 +1,61 @@ +package reader + +import ( + "encoding/hex" + "fmt" + "io/ioutil" + "log" + "os" + "strings" + + "github.com/mash/go-intelhex" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +func init() { + flags := MainCmd.Flags() + + flags.Bool("binary", false, "display binary") + viper.BindPFlag("binary", flags.Lookup("binary")) +} + +// MainCmd is the main command manager +var MainCmd = &cobra.Command{ + Use: "read ", + Short: "Read data from hex file", + Run: func(cmd *cobra.Command, args []string) { + if len(args) == 0 { + log.Fatal("Missing file") + } + + hexFile, err := os.Open(args[0]) + + if err != nil { + log.Fatal(err) //log.Fatal run an os.Exit + } + defer hexFile.Close() + + bytes, err := ioutil.ReadAll(hexFile) + if err != nil { + log.Fatal(err) //log.Fatal run an os.Exit + } + + _, records := intelhex.ParseString(string(bytes)) + for record := range records { + if viper.GetBool("binary") { + src := strings.ToLower(record.Data) + dst, err := hex.DecodeString(src) + if err != nil { + log.Fatal(err) + } + + fmt.Printf("%s", dst) + } else { + fmt.Printf("%s\n", record.Data) + } + + } + + }, +} diff --git a/intelhex/writer/writer.go b/intelhex/writer/writer.go new file mode 100644 index 0000000..418f92d --- /dev/null +++ b/intelhex/writer/writer.go @@ -0,0 +1,83 @@ +package writer + +import ( + "bufio" + "fmt" + "log" + "os" + "strings" + "encoding/hex" + + "github.com/spf13/cobra" + "github.com/spf13/viper" + "github.com/mash/go-intelhex" +) + +func init() { + flags := MainCmd.Flags() + + flags.Bool("immediate", false, "immediate string is encoded") + viper.BindPFlag("immediate", flags.Lookup("immediate")) +} + +// MainCmd is the main command manager +var MainCmd = &cobra.Command{ + Use: "write ", + Short: "Write data from binary file", + Run: func(cmd *cobra.Command, args []string) { + if len(args) == 0 { + log.Fatal("Missing file/immediate") + } + + if viper.GetBool("immediate") { + str := args[0] + + strB := []byte(str) + encodedStr := hex.EncodeToString(strB) + + record := intelhex.Record{ + ByteCount : int64(len(str)), + Address : int64(0), + Type : intelhex.RecordTypeData, + Data : strings.ToUpper(encodedStr), + } + + fmt.Printf("%s:00000001FF\n", string(record.Format(16))) + + } else { + binFile, err := os.Open(args[0]) + + if err != nil { + log.Fatal(err) //log.Fatal run an os.Exit + } + defer binFile.Close() + + stats, statsErr := binFile.Stat() + if statsErr != nil { + log.Fatal("Cannot read file") + } + + var size int64 = stats.Size() + bytes := make([]byte, size) + + bufr := bufio.NewReader(binFile) + _,err = bufr.Read(bytes) + if err != nil { + log.Fatal(err) //log.Fatal run an os.Exit + } + + encodedStr := hex.EncodeToString(bytes) + + record := intelhex.Record{ + ByteCount : int64(size), + Address : int64(0), + Type : intelhex.RecordTypeData, + Data : strings.ToUpper(encodedStr), + } + + fmt.Printf("%s:00000001FF\n", string(record.Format(16))) + + } + + }, +}