|
| 1 | +package codesys2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/binary" |
| 6 | +) |
| 7 | + |
| 8 | +const CodeSysV2Magic = 0xbbbb |
| 9 | +const HeaderSize = 6 |
| 10 | + |
| 11 | +type CodeSysV2Header struct { |
| 12 | + Magic uint16 |
| 13 | + Length uint32 |
| 14 | +} |
| 15 | + |
| 16 | +func (header *CodeSysV2Header) New() { |
| 17 | + header.Magic = CodeSysV2Magic |
| 18 | +} |
| 19 | + |
| 20 | +func (header *CodeSysV2Header) SetHeaderSize(payload any) { |
| 21 | + data, err := Marshal(payload, binary.BigEndian) |
| 22 | + if err == nil { |
| 23 | + header.Length = uint32(len(data) - HeaderSize) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +type CodeSysV2Request struct { |
| 28 | + CodeSysV2Header |
| 29 | + Cmd byte |
| 30 | +} |
| 31 | + |
| 32 | +func (request *CodeSysV2Request) New(cmd byte) { |
| 33 | + request.CodeSysV2Header.New() |
| 34 | + request.Cmd = cmd |
| 35 | +} |
| 36 | + |
| 37 | +const ( |
| 38 | + Login = 0x1 |
| 39 | + |
| 40 | + Logout = 0x2 |
| 41 | + |
| 42 | + Start = 0x3 |
| 43 | + |
| 44 | + Stop = 0x4 |
| 45 | + |
| 46 | + Readvariablelist = 0x5 |
| 47 | + |
| 48 | + Writevariablelist = 0x6 |
| 49 | + |
| 50 | + Enable = 0x7 |
| 51 | + |
| 52 | + Disable = 0x8 |
| 53 | + |
| 54 | + Force = 0x9 |
| 55 | + |
| 56 | + Stepin = 0xa |
| 57 | + |
| 58 | + Stepover = 0xb |
| 59 | + |
| 60 | + Setbreakpoint = 0xc |
| 61 | + |
| 62 | + Deletebreakpoint = 0xd |
| 63 | + |
| 64 | + Deleteallbreakpoints = 0xe |
| 65 | + |
| 66 | + Go = 0xf |
| 67 | + |
| 68 | + Readstatus = 0x10 |
| 69 | + |
| 70 | + Readidentity = 0x11 |
| 71 | + |
| 72 | + Readbreakpointlist = 0x12 |
| 73 | + |
| 74 | + Reset = 0x13 |
| 75 | + |
| 76 | + Definevariablelist = 0x14 |
| 77 | + |
| 78 | + Deletevariablelist = 0x15 |
| 79 | + |
| 80 | + Callstack = 0x17 |
| 81 | + |
| 82 | + Cycle = 0x18 |
| 83 | + |
| 84 | + Defineflowcontrol = 0x19 |
| 85 | + |
| 86 | + Readflowcontrol = 0x1a |
| 87 | + |
| 88 | + Stopflowcontrol = 0x1b |
| 89 | + |
| 90 | + Definetrace = 0x1c |
| 91 | + |
| 92 | + Starttrace = 0x1d |
| 93 | + |
| 94 | + Readtrace = 0x1e |
| 95 | + |
| 96 | + Stoptrace = 0x1f |
| 97 | + |
| 98 | + Forcevariables = 0x20 |
| 99 | + |
| 100 | + Releasevariables = 0x21 |
| 101 | + |
| 102 | + Onlinechange = 0x22 |
| 103 | + |
| 104 | + Startstep = 0x23 |
| 105 | + |
| 106 | + Cyclestep = 0x24 |
| 107 | + |
| 108 | + Defineaccuflow = 0x28 |
| 109 | + |
| 110 | + Definesnapshot = 0x29 |
| 111 | + |
| 112 | + Cancelsnapshot = 0x2a |
| 113 | + |
| 114 | + Exit = 0x2b |
| 115 | + |
| 116 | + ReadWritevariable = 0x2c |
| 117 | + |
| 118 | + Defineconfig = 0x2d |
| 119 | + |
| 120 | + Readvariablesdirect = 0x2e |
| 121 | + |
| 122 | + Filewritestart = 0x2f |
| 123 | + |
| 124 | + Filewritecontinue = 0x30 |
| 125 | + |
| 126 | + Filereadstart = 0x31 |
| 127 | + |
| 128 | + Filereadcontinue = 0x32 |
| 129 | + |
| 130 | + Filereadlist = 0x33 |
| 131 | + |
| 132 | + Filereadinfo = 0x34 |
| 133 | + |
| 134 | + Filerename = 0x35 |
| 135 | + |
| 136 | + Filedelete = 0x36 |
| 137 | + |
| 138 | + Downloadtaskconfig = 0x37 |
| 139 | + |
| 140 | + Definedebugtask = 0x38 |
| 141 | + |
| 142 | + Createbootproject = 0x39 |
| 143 | + |
| 144 | + Downloadsymbols = 0x3a |
| 145 | + |
| 146 | + Readtaskruntimeinfo = 0x3b |
| 147 | + |
| 148 | + Writevariablesdirect = 0x3c |
| 149 | + |
| 150 | + Seteventcycletime = 0x3d |
| 151 | + |
| 152 | + DownloadIODescription = 0x3e |
| 153 | + |
| 154 | + Visualizationready = 0x3f |
| 155 | + |
| 156 | + Downloadprojectinfo = 0x40 |
| 157 | + |
| 158 | + Checkbootproject = 0x41 |
| 159 | + |
| 160 | + Checktargetid = 0x42 |
| 161 | + |
| 162 | + Filetransferdone = 0x43 |
| 163 | + |
| 164 | + Readvariablesex = 0x44 |
| 165 | + |
| 166 | + Writevariablesex = 0x45 |
| 167 | + |
| 168 | + Readvariablesdirectex = 0x46 |
| 169 | + |
| 170 | + Writevariablesdirectex = 0x47 |
| 171 | + |
| 172 | + FileDir = 0x48 |
| 173 | + |
| 174 | + ForceIntracycle = 0x48 |
| 175 | + |
| 176 | + ForceIntracyclePRE = 0x49 |
| 177 | + |
| 178 | + Extendedvariableservice = 0x50 |
| 179 | + |
| 180 | + Extendeddebugservice = 0x51 |
| 181 | + |
| 182 | + GLdownload = 0x64 |
| 183 | + |
| 184 | + GLobserve = 0x65 |
| 185 | + |
| 186 | + GLdownloadblock = 0x66 |
| 187 | + |
| 188 | + Download = 0x80 |
| 189 | + |
| 190 | + Downloadsource = 0x81 |
| 191 | + |
| 192 | + Uploadsource = 0x82 |
| 193 | + |
| 194 | + Flash = 0x83 |
| 195 | + |
| 196 | + Downloadready = 0x8f |
| 197 | + |
| 198 | + Getlasterror = 0x90 |
| 199 | + |
| 200 | + Setpassword = 0x91 |
| 201 | + |
| 202 | + Browsercommand = 0x92 |
| 203 | + |
| 204 | + ODservice = 0x93 |
| 205 | +) |
| 206 | + |
| 207 | +type CodeSysV2LoginRequest struct { |
| 208 | + CodeSysV2Request |
| 209 | + Unknown1 uint32 |
| 210 | + Unknown2 uint32 |
| 211 | + PasswordLength uint32 |
| 212 | +} |
| 213 | + |
| 214 | +// Login request as anonymous user to get information from the device, even if this is not allowed the device will response with information |
| 215 | +func (request *CodeSysV2LoginRequest) New() { |
| 216 | + request.CodeSysV2Request.New(Login) |
| 217 | + request.Unknown1 = 4 |
| 218 | + request.Unknown2 = 6 |
| 219 | + request.PasswordLength = 0 |
| 220 | + request.SetHeaderSize(request) |
| 221 | +} |
| 222 | + |
| 223 | +type CodeSysV2LoginResponse struct { |
| 224 | + CodeSysV2Header |
| 225 | + LoginResult uint16 |
| 226 | + Unknown1 [56]byte |
| 227 | + OsType [28]byte |
| 228 | + Unknown2 uint32 |
| 229 | + OsVersion [32]byte |
| 230 | + Vendor [28]byte |
| 231 | + Unknown3 [56]byte |
| 232 | +} |
| 233 | + |
| 234 | +func Marshal(packet any, byteOrder binary.ByteOrder) ([]byte, error) { |
| 235 | + data := make([]byte, 0, 1024) |
| 236 | + buffer := bytes.NewBuffer(data) |
| 237 | + buffer.Reset() |
| 238 | + err := binary.Write(buffer, byteOrder, packet) |
| 239 | + if err != nil { |
| 240 | + return nil, err |
| 241 | + } |
| 242 | + |
| 243 | + return buffer.Bytes(), err |
| 244 | +} |
| 245 | + |
| 246 | +func UnMarshal(packet []byte, byteOrder binary.ByteOrder, packet_struct any) error { |
| 247 | + buffer := bytes.NewBuffer(packet) |
| 248 | + err := binary.Read(buffer, byteOrder, packet_struct) |
| 249 | + return err |
| 250 | +} |
| 251 | + |
| 252 | +type CodeSysV2DeviceInfo struct { |
| 253 | + // The operation system that runs on the device |
| 254 | + OsType string `json:"os_type"` |
| 255 | + |
| 256 | + // The operation system version that runs on the device |
| 257 | + OsVersion string `json:"os_version"` |
| 258 | + |
| 259 | + // The vendor of the device |
| 260 | + Vendor string `json:"vendor"` |
| 261 | +} |
0 commit comments