tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/lora/lorawan/atcmd/parser.go (about) 1 package main 2 3 import ( 4 "errors" 5 "strings" 6 ) 7 8 var ( 9 errInvalidCommand = errors.New("Invalid command") 10 ) 11 12 func parse(data []byte) error { 13 switch { 14 case len(data) < 2, string(data[0:2]) != "AT": 15 return errInvalidCommand 16 case len(data) == 2: 17 // just the AT command by itself 18 quicktest() 19 case len(data) < 6 || data[2] != '+': 20 return errInvalidCommand 21 default: 22 // parse the rest of the command 23 cmd, args, _ := strings.Cut(string(data[3:]), "=") 24 return parseCommand(cmd, args) 25 } 26 27 return nil 28 } 29 30 func parseCommand(cmd, args string) error { 31 switch cmd { 32 case "VER": 33 version() 34 case "ID": 35 id(args) 36 case "RESET": 37 reset() 38 case "MSG": 39 msg(args) 40 case "CMSG": 41 cmsg(args) 42 case "MSGHEX": 43 msghex(args) 44 case "CMSGHEX": 45 cmsghex(args) 46 case "PMSG": 47 pmsg(args) 48 case "PMSGHEX": 49 pmsg(args) 50 case "PORT": 51 port(args) 52 case "ADR": 53 adr(args) 54 case "DR": 55 dr(args) 56 case "CH": 57 ch(args) 58 case "POWER": 59 power(args) 60 case "REPT": 61 rept(args) 62 case "RETRY": 63 retry(args) 64 case "RXWIN2": 65 rxwin2(args) 66 case "RXWIN1": 67 rxwin1(args) 68 case "KEY": 69 key(args) 70 case "FDEFAULT": 71 fdefault(args) 72 case "MODE": 73 mode(args) 74 case "JOIN": 75 join(args) 76 case "BEACON": 77 join(args) 78 case "CLASS": 79 class(args) 80 case "DELAY": 81 delay(args) 82 case "LW": 83 lw(args) 84 case "WDT": 85 wdt(args) 86 case "LOWPOWER": 87 lowpower(args) 88 case "VDD": 89 vdd(args) 90 case "TEMP": 91 temp(args) 92 case "RTC": 93 rtc(args) 94 case "EEPROM": 95 eeprom(args) 96 case "UART": 97 uartcmd(args) 98 case "TEST": 99 test(args) 100 case "LOG": 101 log(args) 102 case "RECV": 103 recv(args) 104 case "RECVHEX": 105 recvhex(args) 106 case "SEND": 107 send(args) 108 case "SENDHEX": 109 sendhex(args) 110 default: 111 return errInvalidCommand 112 } 113 114 return nil 115 }