|
@@ -1,82 +1,87 @@
|
|
|
package main
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
|
+ "flag"
|
|
|
"fmt"
|
|
"fmt"
|
|
|
"log"
|
|
"log"
|
|
|
"net"
|
|
"net"
|
|
|
"os"
|
|
"os"
|
|
|
- "strconv"
|
|
|
|
|
"strings"
|
|
"strings"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
func main() {
|
|
|
- const BrotherPort int = 54921
|
|
|
|
|
- BrotherIP := os.Args[1]
|
|
|
|
|
- resolution := os.Args[2]
|
|
|
|
|
- color := os.Args[3]
|
|
|
|
|
-
|
|
|
|
|
- if net.ParseIP(BrotherIP) == nil {
|
|
|
|
|
- fmt.Fprintf(os.Stderr, "Invalid IP address. Usage: %s <scanner ip>", os.Args[0])
|
|
|
|
|
- os.Exit(0)
|
|
|
|
|
|
|
+ const brotherPort int = 54921
|
|
|
|
|
+
|
|
|
|
|
+ brotherIP := flag.String("a", "192.168.0.157", "IP address of the Brother scanner")
|
|
|
|
|
+ resolution := flag.Int("r", 300, "Resolution of the scan")
|
|
|
|
|
+ color := flag.String("c", "CGRAY", "Color mode of the scan")
|
|
|
|
|
+ adf := flag.Bool("m", false, "Enable scan of all pages from feeder")
|
|
|
|
|
+
|
|
|
|
|
+ flag.Parse()
|
|
|
|
|
+
|
|
|
|
|
+ if net.ParseIP(*brotherIP) == nil {
|
|
|
|
|
+ HandleError(fmt.Errorf("Invalid IP address: %s", *brotherIP))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
log.Println("Valid IP address, opening socket...")
|
|
log.Println("Valid IP address, opening socket...")
|
|
|
|
|
|
|
|
- conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", BrotherIP, BrotherPort))
|
|
|
|
|
- handleError(err)
|
|
|
|
|
|
|
+ socket, err := net.Dial("tcp", fmt.Sprintf("%s:%d", *brotherIP, brotherPort))
|
|
|
|
|
|
|
|
- sendRequest(conn, resolution, color)
|
|
|
|
|
|
|
+ HandleError(err)
|
|
|
|
|
|
|
|
- defer conn.Close()
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ defer socket.Close()
|
|
|
|
|
|
|
|
-func sendRequest(conn net.Conn, _resolution string, _mode string) {
|
|
|
|
|
|
|
+ SendRequest(socket, *resolution, *color, *adf)
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- reply := make([]byte, 128)
|
|
|
|
|
|
|
+func SendRequest(socket net.Conn, resolution int, _mode string, adf bool) {
|
|
|
|
|
|
|
|
- resolution := checkResolution(_resolution)
|
|
|
|
|
- mode, compression := checkMode(_mode)
|
|
|
|
|
|
|
+ mode, compression := GetCompressionMode(_mode)
|
|
|
|
|
|
|
|
log.Println("Reading scanner status...")
|
|
log.Println("Reading scanner status...")
|
|
|
|
|
|
|
|
- _, err := conn.Read(reply)
|
|
|
|
|
- handleError(err)
|
|
|
|
|
|
|
+ status := ReadPacket(socket)[:7]
|
|
|
|
|
|
|
|
- if "+OK 200" != string(reply[:7]) {
|
|
|
|
|
- handleError(fmt.Errorf("Invalid reply from scanner: %s", reply))
|
|
|
|
|
|
|
+ if "+OK 200" != status {
|
|
|
|
|
+ HandleError(fmt.Errorf("Invalid reply from scanner: %s", status))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
log.Println("Leasing options...")
|
|
log.Println("Leasing options...")
|
|
|
|
|
|
|
|
- resp := []byte(fmt.Sprintf("\x1bI\nR=%d,%d\nM=%s\n\x80", resolution, resolution, mode))
|
|
|
|
|
- _, err = conn.Write(resp)
|
|
|
|
|
- handleError(err)
|
|
|
|
|
|
|
+ request := []byte(fmt.Sprintf("\x1bI\nR=%d,%d\nM=%s\n\x80", resolution, resolution, mode))
|
|
|
|
|
+ SendPacket(socket, request)
|
|
|
|
|
|
|
|
- _, err = conn.Read(reply)
|
|
|
|
|
- handleError(err)
|
|
|
|
|
|
|
+ offer := ReadPacket(socket)
|
|
|
|
|
|
|
|
- response := strings.Split(string(reply), ",")
|
|
|
|
|
|
|
+ if adf {
|
|
|
|
|
+ log.Println("Enabling automatic document feeder (ADF)")
|
|
|
|
|
+
|
|
|
|
|
+ request = []byte("\x1bD\nADF\n\x80")
|
|
|
|
|
+ SendPacket(socket, request)
|
|
|
|
|
+
|
|
|
|
|
+ ReadPacket(socket)
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
log.Println("Sending scan request...")
|
|
log.Println("Sending scan request...")
|
|
|
|
|
|
|
|
- scanRequestFormat := "\x1bX\nR=%v,%v\nM=%s\nC=%s\nJ=MID\nB=50\nN=50\nA=0,0,%v,%v\n\x80"
|
|
|
|
|
|
|
+ offerOptions := strings.Split(offer, ",")
|
|
|
|
|
|
|
|
- resp = []byte(fmt.Sprintf(scanRequestFormat, response[1], response[1], mode, compression, response[5], response[6]))
|
|
|
|
|
- _, err = conn.Write(resp)
|
|
|
|
|
- handleError(err)
|
|
|
|
|
|
|
+ requestFormat := "\x1bX\nR=%v,%v\nM=%s\nC=%s\nJ=MID\nB=50\nN=50\nA=0,0,%v,%v\n\x80"
|
|
|
|
|
+ request = []byte(fmt.Sprintf(requestFormat, offerOptions[1], offerOptions[1], mode, compression, offerOptions[5], offerOptions[6]))
|
|
|
|
|
+ SendPacket(socket, request)
|
|
|
|
|
+
|
|
|
|
|
+ log.Println("Scanning started...")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func checkResolution(_resolution string) int {
|
|
|
|
|
- resolution, err := strconv.Atoi(_resolution)
|
|
|
|
|
|
|
+func HandleError(err error) {
|
|
|
|
|
|
|
|
- if err != nil && resolution < 100 {
|
|
|
|
|
- resolution = 300
|
|
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ log.Fatal(err)
|
|
|
|
|
+ os.Exit(1)
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return resolution
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func checkMode(_mode string) (string, string) {
|
|
|
|
|
|
|
+func GetCompressionMode(_mode string) (string, string) {
|
|
|
|
|
|
|
|
if _mode == "GRAY64" {
|
|
if _mode == "GRAY64" {
|
|
|
return _mode, "NONE"
|
|
return _mode, "NONE"
|
|
@@ -85,10 +90,16 @@ func checkMode(_mode string) (string, string) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func handleError(err error) {
|
|
|
|
|
|
|
+func SendPacket(socket net.Conn, packet []byte) {
|
|
|
|
|
+ _, err := socket.Write(packet)
|
|
|
|
|
+ HandleError(err)
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- log.Fatal(err)
|
|
|
|
|
- os.Exit(1)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+func ReadPacket(socket net.Conn) string {
|
|
|
|
|
+ reply := make([]byte, 128)
|
|
|
|
|
+
|
|
|
|
|
+ _, err := socket.Read(reply)
|
|
|
|
|
+ HandleError(err)
|
|
|
|
|
+
|
|
|
|
|
+ return string(reply)
|
|
|
}
|
|
}
|