formatting
This commit is contained in:
@@ -75,7 +75,7 @@ static const struct argp_option g_mqtt_argp_options[] = {
|
|||||||
{"mqttcert", O_CERT, "CERTFILE", 0, "Use CERTFILE for MQTT TLS client certificate (no default)", 0 },
|
{"mqttcert", O_CERT, "CERTFILE", 0, "Use CERTFILE for MQTT TLS client certificate (no default)", 0 },
|
||||||
{"mqttkey", O_KEYF, "KEYFILE", 0, "Use KEYFILE for MQTT TLS client certificate (no default)", 0 },
|
{"mqttkey", O_KEYF, "KEYFILE", 0, "Use KEYFILE for MQTT TLS client certificate (no default)", 0 },
|
||||||
{"mqttkeypass", O_KEPA, "PASSWORD", 0, "Use PASSWORD for the encrypted KEYFILE (no default)", 0 },
|
{"mqttkeypass", O_KEPA, "PASSWORD", 0, "Use PASSWORD for the encrypted KEYFILE (no default)", 0 },
|
||||||
{"mqttinsecure",O_INSE, nullptr, 0, "Allow insecure TLS connection (e.g. using a self signed certificate)", 0 },
|
{"mqttinsecure", O_INSE, nullptr, 0, "Allow insecure TLS connection (e.g. using a self signed certificate)", 0 },
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
{nullptr, 0, nullptr, 0, nullptr, 0 },
|
{nullptr, 0, nullptr, 0, nullptr, 0 },
|
||||||
@@ -251,7 +251,7 @@ static error_t mqtt_parse_opt(int key, char *arg, struct argp_state *state) {
|
|||||||
}
|
}
|
||||||
g_keypass = replaceSecret(arg);
|
g_keypass = replaceSecret(arg);
|
||||||
break;
|
break;
|
||||||
case O_INSE: //--mqttinsecure
|
case O_INSE: // --mqttinsecure
|
||||||
g_insecure = true;
|
g_insecure = true;
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
@@ -647,11 +647,11 @@ void MqttHandler::notifyTopic(const string& topic, const string& data) {
|
|||||||
if (isList) {
|
if (isList) {
|
||||||
logOtherInfo("mqtt", "received list topic for %s %s", circuit.c_str(), name.c_str());
|
logOtherInfo("mqtt", "received list topic for %s %s", circuit.c_str(), name.c_str());
|
||||||
deque<Message*> messages;
|
deque<Message*> messages;
|
||||||
bool circuitPrefix = circuit.length()>0 && circuit.find_last_of('*') == circuit.length()-1;
|
bool circuitPrefix = circuit.length() > 0 && circuit.find_last_of('*') == circuit.length()-1;
|
||||||
if (circuitPrefix) {
|
if (circuitPrefix) {
|
||||||
circuit = circuit.substr(0, circuit.length()-1);
|
circuit = circuit.substr(0, circuit.length()-1);
|
||||||
}
|
}
|
||||||
bool namePrefix = name.length()>0 && name.find_last_of('*') == name.length()-1;
|
bool namePrefix = name.length() > 0 && name.find_last_of('*') == name.length()-1;
|
||||||
if (namePrefix) {
|
if (namePrefix) {
|
||||||
name = name.substr(0, name.length()-1);
|
name = name.substr(0, name.length()-1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ class Device {
|
|||||||
* @param enhancedProto whether to use the ebusd enhanced protocol.
|
* @param enhancedProto whether to use the ebusd enhanced protocol.
|
||||||
*/
|
*/
|
||||||
Device(const char* name, bool checkDevice, unsigned int latency, bool readOnly, bool initialSend,
|
Device(const char* name, bool checkDevice, unsigned int latency, bool readOnly, bool initialSend,
|
||||||
bool enhancedProto=false);
|
bool enhancedProto = false);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
@@ -174,7 +174,7 @@ class Device {
|
|||||||
* Return whether the device is currently in arbitration.
|
* Return whether the device is currently in arbitration.
|
||||||
* @return true when the device is currently in arbitration.
|
* @return true when the device is currently in arbitration.
|
||||||
*/
|
*/
|
||||||
bool isArbitrating() const { return m_arbitrationMaster != SYN; };
|
bool isArbitrating() const { return m_arbitrationMaster != SYN; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the device name.
|
* Return the device name.
|
||||||
@@ -220,7 +220,7 @@ class Device {
|
|||||||
* @param startArbitration true to start arbitration.
|
* @param startArbitration true to start arbitration.
|
||||||
* @return true on success, false on error.
|
* @return true on success, false on error.
|
||||||
*/
|
*/
|
||||||
virtual bool write(symbol_t value, bool startArbitration=false);
|
virtual bool write(symbol_t value, bool startArbitration = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether a symbol is available for reading immediately (without waiting).
|
* Check whether a symbol is available for reading immediately (without waiting).
|
||||||
@@ -236,7 +236,8 @@ class Device {
|
|||||||
* @param incomplete the variable in which to store when a partial transfer needs another poll.
|
* @param incomplete the variable in which to store when a partial transfer needs another poll.
|
||||||
* @return true on success, false on error.
|
* @return true on success, false on error.
|
||||||
*/
|
*/
|
||||||
virtual bool read(symbol_t* value, bool isAvailable, ArbitrationState* arbitrationState=nullptr, bool* incomplete=nullptr);
|
virtual bool read(symbol_t* value, bool isAvailable, ArbitrationState* arbitrationState = nullptr,
|
||||||
|
bool* incomplete = nullptr);
|
||||||
|
|
||||||
/** the device name (e.g. "/dev/ttyUSB0" for serial, "127.0.0.1:1234" for network). */
|
/** the device name (e.g. "/dev/ttyUSB0" for serial, "127.0.0.1:1234" for network). */
|
||||||
const char* m_name;
|
const char* m_name;
|
||||||
@@ -299,7 +300,7 @@ class SerialDevice : public Device {
|
|||||||
* @param enhancedProto whether to use the ebusd enhanced protocol.
|
* @param enhancedProto whether to use the ebusd enhanced protocol.
|
||||||
*/
|
*/
|
||||||
SerialDevice(const char* name, bool checkDevice, unsigned int extraLatency, bool readOnly, bool initialSend,
|
SerialDevice(const char* name, bool checkDevice, unsigned int extraLatency, bool readOnly, bool initialSend,
|
||||||
bool enhancedProto=false)
|
bool enhancedProto = false)
|
||||||
: Device(name, checkDevice, extraLatency, readOnly, initialSend, enhancedProto) {}
|
: Device(name, checkDevice, extraLatency, readOnly, initialSend, enhancedProto) {}
|
||||||
|
|
||||||
// @copydoc
|
// @copydoc
|
||||||
@@ -337,7 +338,7 @@ class NetworkDevice : public Device {
|
|||||||
* @param enhancedProto whether to use the ebusd enhanced protocol.
|
* @param enhancedProto whether to use the ebusd enhanced protocol.
|
||||||
*/
|
*/
|
||||||
NetworkDevice(const char* name, const char* hostOrIp, uint16_t port, unsigned int extraLatency, bool readOnly,
|
NetworkDevice(const char* name, const char* hostOrIp, uint16_t port, unsigned int extraLatency, bool readOnly,
|
||||||
bool initialSend, bool udp, bool enhancedProto=false)
|
bool initialSend, bool udp, bool enhancedProto = false)
|
||||||
: Device(name, true, NETWORK_LATENCY_MS+extraLatency, readOnly, initialSend, enhancedProto),
|
: Device(name, true, NETWORK_LATENCY_MS+extraLatency, readOnly, initialSend, enhancedProto),
|
||||||
m_hostOrIp(hostOrIp), m_port(port), m_udp(udp) {}
|
m_hostOrIp(hostOrIp), m_port(port), m_udp(udp) {}
|
||||||
|
|
||||||
|
|||||||
+65
-55
@@ -258,15 +258,15 @@ ssize_t waitWrite(int fd, uint8_t *data, size_t len, int timeoutMillis) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
ret = write(fd, data, len);
|
ret = write(fd, data, len);
|
||||||
if (ret<0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
#ifdef DEBUG_RAW
|
#ifdef DEBUG_RAW
|
||||||
std::cout<<"> "<<std::dec<<static_cast<unsigned>(ret)<<"/"<<static_cast<unsigned>(len)<<":"<<std::hex;
|
std::cout << "> " << std::dec << static_cast<unsigned>(ret) << "/" << static_cast<unsigned>(len) << ":" << std::hex;
|
||||||
for (int pos = 0; pos<ret; pos++) {
|
for (int pos = 0; pos < ret; pos++) {
|
||||||
std::cout<<" "<<std::setw(2)<<std::setfill('0')<<static_cast<unsigned>(data[pos]);
|
std::cout << " " << std::setw(2) << std::setfill('0') << static_cast<unsigned>(data[pos]);
|
||||||
}
|
}
|
||||||
std::cout<<std::endl;
|
std::cout << std::endl;
|
||||||
#endif
|
#endif
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -284,21 +284,21 @@ ssize_t waitRead(int fd, uint8_t *data, size_t len, int timeoutMillis) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
ret = read(fd, data, len);
|
ret = read(fd, data, len);
|
||||||
if (ret<0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
#ifdef DEBUG_RAW
|
#ifdef DEBUG_RAW
|
||||||
std::cout<<"< "<<std::dec<<static_cast<unsigned>(ret)<<"/"<<static_cast<unsigned>(len)<<":"<<std::hex;
|
std::cout << "< " << std::dec << static_cast<unsigned>(ret) << "/" << static_cast<unsigned>(len) << ":" << std::hex;
|
||||||
for (int pos = 0; pos<ret; pos++) {
|
for (int pos = 0; pos < ret; pos++) {
|
||||||
std::cout<<" "<<std::setw(2)<<std::setfill('0')<<static_cast<unsigned>(data[pos]);
|
std::cout << " " << std::setw(2) << std::setfill('0') << static_cast<unsigned>(data[pos]);
|
||||||
}
|
}
|
||||||
std::cout<<std::endl;
|
std::cout << std::endl;
|
||||||
#endif
|
#endif
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t sendReceiveFrame(int fd, frame_t& frame, size_t sendDataLen, ssize_t fixReceiveDataLen,
|
ssize_t sendReceiveFrame(int fd, frame_t& frame, size_t sendDataLen, ssize_t fixReceiveDataLen,
|
||||||
int responseTimeoutExtraMillis=0, bool hideErrors=false) {
|
int responseTimeoutExtraMillis = 0, bool hideErrors = false) {
|
||||||
// send 0x55 for auto baud detection in PIC
|
// send 0x55 for auto baud detection in PIC
|
||||||
unsigned char ch = STX;
|
unsigned char ch = STX;
|
||||||
ssize_t cnt = waitWrite(fd, &ch, 1, WAIT_BYTE_TRANSFERRED_MILLIS);
|
ssize_t cnt = waitWrite(fd, &ch, 1, WAIT_BYTE_TRANSFERRED_MILLIS);
|
||||||
@@ -318,9 +318,9 @@ ssize_t sendReceiveFrame(int fd, frame_t& frame, size_t sendDataLen, ssize_t fix
|
|||||||
usleep(WAIT_BITRATE_DETECTION_MICROS);
|
usleep(WAIT_BITRATE_DETECTION_MICROS);
|
||||||
uint8_t writeCommand = frame.command;
|
uint8_t writeCommand = frame.command;
|
||||||
size_t len = FRAME_HEADER_LEN+sendDataLen;
|
size_t len = FRAME_HEADER_LEN+sendDataLen;
|
||||||
for (size_t pos=0; pos<len; ) {
|
for (size_t pos=0; pos < len; ) {
|
||||||
cnt = waitWrite(fd, frame.buffer+pos, len-pos, WAIT_BYTE_TRANSFERRED_MILLIS);
|
cnt = waitWrite(fd, frame.buffer+pos, len-pos, WAIT_BYTE_TRANSFERRED_MILLIS);
|
||||||
if (cnt<0) {
|
if (cnt < 0) {
|
||||||
if (!hideErrors) {
|
if (!hideErrors) {
|
||||||
std::cerr << "write data failed" << std::endl;
|
std::cerr << "write data failed" << std::endl;
|
||||||
}
|
}
|
||||||
@@ -335,7 +335,7 @@ ssize_t sendReceiveFrame(int fd, frame_t& frame, size_t sendDataLen, ssize_t fix
|
|||||||
pos += cnt;
|
pos += cnt;
|
||||||
}
|
}
|
||||||
cnt = waitRead(fd, &ch, 1, WAIT_RESPONSE_TIMEOUT_MILLIS + responseTimeoutExtraMillis);
|
cnt = waitRead(fd, &ch, 1, WAIT_RESPONSE_TIMEOUT_MILLIS + responseTimeoutExtraMillis);
|
||||||
if (cnt<0) {
|
if (cnt < 0) {
|
||||||
if (!hideErrors) {
|
if (!hideErrors) {
|
||||||
std::cerr << "read sync failed" << std::endl;
|
std::cerr << "read sync failed" << std::endl;
|
||||||
}
|
}
|
||||||
@@ -356,9 +356,9 @@ ssize_t sendReceiveFrame(int fd, frame_t& frame, size_t sendDataLen, ssize_t fix
|
|||||||
}
|
}
|
||||||
// read the answer from the device
|
// read the answer from the device
|
||||||
len = FRAME_HEADER_LEN; // start with the header itself
|
len = FRAME_HEADER_LEN; // start with the header itself
|
||||||
for (size_t pos=0; pos<len; ) {
|
for (size_t pos=0; pos < len; ) {
|
||||||
cnt = waitRead(fd, frame.buffer+pos, len-pos, WAIT_BYTE_TRANSFERRED_MILLIS);
|
cnt = waitRead(fd, frame.buffer+pos, len-pos, WAIT_BYTE_TRANSFERRED_MILLIS);
|
||||||
if (cnt<0) {
|
if (cnt < 0) {
|
||||||
if (!hideErrors) {
|
if (!hideErrors) {
|
||||||
std::cerr << "read data failed" << std::endl;
|
std::cerr << "read data failed" << std::endl;
|
||||||
}
|
}
|
||||||
@@ -372,7 +372,7 @@ ssize_t sendReceiveFrame(int fd, frame_t& frame, size_t sendDataLen, ssize_t fix
|
|||||||
}
|
}
|
||||||
pos += cnt;
|
pos += cnt;
|
||||||
if (pos == FRAME_HEADER_LEN) {
|
if (pos == FRAME_HEADER_LEN) {
|
||||||
if (fixReceiveDataLen<0) {
|
if (fixReceiveDataLen < 0) {
|
||||||
len += frame.data_length;
|
len += frame.data_length;
|
||||||
} else {
|
} else {
|
||||||
len += fixReceiveDataLen;
|
len += fixReceiveDataLen;
|
||||||
@@ -391,7 +391,7 @@ ssize_t sendReceiveFrame(int fd, frame_t& frame, size_t sendDataLen, ssize_t fix
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int readVersion(int fd, bool verbose=true) {
|
int readVersion(int fd, bool verbose = true) {
|
||||||
frame_t frame;
|
frame_t frame;
|
||||||
memset(frame.buffer, 0, FRAME_MAX_LEN);
|
memset(frame.buffer, 0, FRAME_MAX_LEN);
|
||||||
frame.command = READ_VERSION;
|
frame.command = READ_VERSION;
|
||||||
@@ -406,7 +406,8 @@ int readVersion(int fd, bool verbose=true) {
|
|||||||
if (verbose) {
|
if (verbose) {
|
||||||
std::cout << "Max packet size: " << static_cast<unsigned>(frame.data[2] | (frame.data[3] << 8)) << std::endl;
|
std::cout << "Max packet size: " << static_cast<unsigned>(frame.data[2] | (frame.data[3] << 8)) << std::endl;
|
||||||
}
|
}
|
||||||
std::cout << "Device ID: " << std::setfill('0') << std::setw(4) << std::hex << static_cast<unsigned>(frame.data[6] | (frame.data[7] << 8));
|
std::cout << "Device ID: " << std::setfill('0') << std::setw(4) << std::hex
|
||||||
|
<< static_cast<unsigned>(frame.data[6] | (frame.data[7] << 8));
|
||||||
if (frame.data[6] == 0xb0 && frame.data[7] == 0x30) {
|
if (frame.data[6] == 0xb0 && frame.data[7] == 0x30) {
|
||||||
std::cout << " (PIC16F15356)";
|
std::cout << " (PIC16F15356)";
|
||||||
}
|
}
|
||||||
@@ -414,10 +415,14 @@ int readVersion(int fd, bool verbose=true) {
|
|||||||
if (verbose) {
|
if (verbose) {
|
||||||
std::cout << "Blocksize erase: " << std::dec << static_cast<unsigned>(frame.data[10]) << std::endl;
|
std::cout << "Blocksize erase: " << std::dec << static_cast<unsigned>(frame.data[10]) << std::endl;
|
||||||
std::cout << "Blocksize write: " << std::dec << static_cast<unsigned>(frame.data[11]) << std::endl;
|
std::cout << "Blocksize write: " << std::dec << static_cast<unsigned>(frame.data[11]) << std::endl;
|
||||||
std::cout << "User ID 1: " << std::setfill('0') << std::setw(2) << std::hex << static_cast<unsigned>(frame.data[12]) << std::endl;
|
std::cout << "User ID 1: " << std::setfill('0') << std::setw(2) << std::hex
|
||||||
std::cout << "User ID 2: " << std::setfill('0') << std::setw(2) << std::hex << static_cast<unsigned>(frame.data[13]) << std::endl;
|
<< static_cast<unsigned>(frame.data[12]) << std::endl;
|
||||||
std::cout << "User ID 3: " << std::setfill('0') << std::setw(2) << std::hex << static_cast<unsigned>(frame.data[14]) << std::endl;
|
std::cout << "User ID 2: " << std::setfill('0') << std::setw(2) << std::hex
|
||||||
std::cout << "User ID 4: " << std::setfill('0') << std::setw(2) << std::hex << static_cast<unsigned>(frame.data[15]) << std::endl;
|
<< static_cast<unsigned>(frame.data[13]) << std::endl;
|
||||||
|
std::cout << "User ID 3: " << std::setfill('0') << std::setw(2) << std::hex
|
||||||
|
<< static_cast<unsigned>(frame.data[14]) << std::endl;
|
||||||
|
std::cout << "User ID 4: " << std::setfill('0') << std::setw(2) << std::hex
|
||||||
|
<< static_cast<unsigned>(frame.data[15]) << std::endl;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -426,14 +431,14 @@ int printFrameData(frame_t frame, bool skipHigh) {
|
|||||||
uint16_t address = (frame.address_H << 8)|frame.address_L;
|
uint16_t address = (frame.address_H << 8)|frame.address_L;
|
||||||
int pos;
|
int pos;
|
||||||
std::cout << std::hex;
|
std::cout << std::hex;
|
||||||
for (pos = 0; pos<frame.data_length;) {
|
for (pos = 0; pos < frame.data_length;) {
|
||||||
if ((pos%16) == 0) {
|
if ((pos%16) == 0) {
|
||||||
std::cout << std::setw(4) << static_cast<unsigned>(address) << ":";
|
std::cout << std::setw(4) << static_cast<unsigned>(address) << ":";
|
||||||
}
|
}
|
||||||
std::cout << " " << std::setw(2) << static_cast<unsigned>(frame.data[pos++]);
|
std::cout << " " << std::setw(2) << static_cast<unsigned>(frame.data[pos++]);
|
||||||
if (skipHigh) {
|
if (skipHigh) {
|
||||||
pos++;
|
pos++;
|
||||||
} else if (pos<frame.data_length) {
|
} else if (pos < frame.data_length) {
|
||||||
std::cout << " " << std::setw(2) << static_cast<unsigned>(frame.data[pos++]);
|
std::cout << " " << std::setw(2) << static_cast<unsigned>(frame.data[pos++]);
|
||||||
}
|
}
|
||||||
address++;
|
address++;
|
||||||
@@ -448,10 +453,12 @@ int printFrameData(frame_t frame, bool skipHigh) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int printFrame(frame_t frame) {
|
int printFrame(frame_t frame) {
|
||||||
std::cout << "command: 0x" << std::setfill('0') << std::setw(2) << std::hex << static_cast<unsigned>(frame.command) << std::endl;
|
std::cout << "command: 0x" << std::setfill('0') << std::setw(2) << std::hex
|
||||||
|
<< static_cast<unsigned>(frame.command) << std::endl;
|
||||||
std::cout << "data_length: " << std::dec << static_cast<unsigned>(frame.data_length) << std::endl;
|
std::cout << "data_length: " << std::dec << static_cast<unsigned>(frame.data_length) << std::endl;
|
||||||
std::cout << "address: 0x" << std::setw(2) << std::hex << static_cast<unsigned>(frame.address_H) << std::setw(2) << std::hex << static_cast<unsigned>(frame.address_L);
|
std::cout << "address: 0x" << std::setw(2) << std::hex << static_cast<unsigned>(frame.address_H) << std::setw(2)
|
||||||
for (int pos = 0; pos<frame.data_length; ) {
|
<< std::hex << static_cast<unsigned>(frame.address_L);
|
||||||
|
for (int pos = 0; pos < frame.data_length; ) {
|
||||||
if ((pos%16) == 0) {
|
if ((pos%16) == 0) {
|
||||||
std::cout << std::endl << std::setw(4) << static_cast<unsigned>(pos) << ":" << std::endl;
|
std::cout << std::endl << std::setw(4) << static_cast<unsigned>(pos) << ":" << std::endl;
|
||||||
}
|
}
|
||||||
@@ -462,7 +469,8 @@ int printFrame(frame_t frame) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int readConfig(int fd, uint16_t address, uint16_t len, bool skipHigh=false, bool print=true, uint8_t* storeData=nullptr) {
|
int readConfig(int fd, uint16_t address, uint16_t len, bool skipHigh = false, bool print = true,
|
||||||
|
uint8_t* storeData = nullptr) {
|
||||||
frame_t frame;
|
frame_t frame;
|
||||||
memset(frame.buffer, 0, FRAME_MAX_LEN);
|
memset(frame.buffer, 0, FRAME_MAX_LEN);
|
||||||
frame.command = READ_CONFIG;
|
frame.command = READ_CONFIG;
|
||||||
@@ -502,7 +510,7 @@ int writeConfig(int fd, uint16_t address, uint16_t len, uint8_t* data) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int readFlash(int fd, uint16_t address, bool skipHigh=false, bool print=true, uint8_t* storeData=nullptr) {
|
int readFlash(int fd, uint16_t address, bool skipHigh = false, bool print = true, uint8_t* storeData = nullptr) {
|
||||||
frame_t frame;
|
frame_t frame;
|
||||||
memset(frame.buffer, 0, FRAME_MAX_LEN);
|
memset(frame.buffer, 0, FRAME_MAX_LEN);
|
||||||
frame.command = READ_FLASH;
|
frame.command = READ_FLASH;
|
||||||
@@ -522,7 +530,7 @@ int readFlash(int fd, uint16_t address, bool skipHigh=false, bool print=true, ui
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int writeFlash(int fd, uint16_t address, uint16_t len, uint8_t* data, bool hideErrors=false) {
|
int writeFlash(int fd, uint16_t address, uint16_t len, uint8_t* data, bool hideErrors = false) {
|
||||||
frame_t frame;
|
frame_t frame;
|
||||||
memset(frame.buffer, 0, FRAME_MAX_LEN);
|
memset(frame.buffer, 0, FRAME_MAX_LEN);
|
||||||
frame.command = WRITE_FLASH;
|
frame.command = WRITE_FLASH;
|
||||||
@@ -617,7 +625,7 @@ int openSerial(std::string port) {
|
|||||||
close(fd);
|
close(fd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
termios.c_iflag |= 0;//IGNPAR;//IGNBRK|IGNPAR|IGNCR;
|
termios.c_iflag |= 0;
|
||||||
termios.c_oflag |= 0;
|
termios.c_oflag |= 0;
|
||||||
termios.c_cflag |= CS8 | CREAD | CLOCAL;
|
termios.c_cflag |= CS8 | CREAD | CLOCAL;
|
||||||
termios.c_lflag |= 0;
|
termios.c_lflag |= 0;
|
||||||
@@ -636,7 +644,7 @@ void closeSerial(int fd) {
|
|||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
int calcFileChecksum(uint8_t* storeFirstBlock=nullptr) {
|
int calcFileChecksum(uint8_t* storeFirstBlock = nullptr) {
|
||||||
std::ifstream inStream;
|
std::ifstream inStream;
|
||||||
inStream.open(flashFile, ifstream::in);
|
inStream.open(flashFile, ifstream::in);
|
||||||
if (!inStream.good()) {
|
if (!inStream.good()) {
|
||||||
@@ -645,7 +653,7 @@ int calcFileChecksum(uint8_t* storeFirstBlock=nullptr) {
|
|||||||
}
|
}
|
||||||
intelhex ih;
|
intelhex ih;
|
||||||
inStream >> ih;
|
inStream >> ih;
|
||||||
if (ih.getNoErrors()>0 || ih.getNoWarnings()>0) {
|
if (ih.getNoErrors() > 0 || ih.getNoWarnings() > 0) {
|
||||||
std::cerr << "unable to read file" << std::endl;
|
std::cerr << "unable to read file" << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -654,7 +662,7 @@ int calcFileChecksum(uint8_t* storeFirstBlock=nullptr) {
|
|||||||
std::cerr << "unable to read file" << std::endl;
|
std::cerr << "unable to read file" << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (startAddr<END_BOOT_BYTES || endAddr>=END_FLASH_BYTES || endAddr<startAddr || (startAddr&0xf) != 0) {
|
if (startAddr < END_BOOT_BYTES || endAddr >= END_FLASH_BYTES || endAddr < startAddr || (startAddr&0xf) != 0) {
|
||||||
std::cerr << "invalid address range" << std::endl;
|
std::cerr << "invalid address range" << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -666,14 +674,14 @@ int calcFileChecksum(uint8_t* storeFirstBlock=nullptr) {
|
|||||||
}
|
}
|
||||||
unsigned long blockStart = END_BOOT_BYTES;
|
unsigned long blockStart = END_BOOT_BYTES;
|
||||||
uint16_t checkSum = 0;
|
uint16_t checkSum = 0;
|
||||||
while (blockStart<END_FLASH_BYTES && nextAddr<END_FLASH_BYTES) {
|
while (blockStart < END_FLASH_BYTES && nextAddr < END_FLASH_BYTES) {
|
||||||
for (int pos = 0; pos < WRITE_FLASH_BLOCKSIZE; pos++, nextAddr++) {
|
for (int pos = 0; pos < WRITE_FLASH_BLOCKSIZE; pos++, nextAddr++) {
|
||||||
unsigned long addr = ih.currentAddress();
|
unsigned long addr = ih.currentAddress();
|
||||||
uint8_t value = (pos&0x1) == 1 ? 0x3f : 0xff;
|
uint8_t value = (pos&0x1) == 1 ? 0x3f : 0xff;
|
||||||
if (addr == nextAddr && ih.getData(&value)) {
|
if (addr == nextAddr && ih.getData(&value)) {
|
||||||
ih.incrementAddress();
|
ih.incrementAddress();
|
||||||
}
|
}
|
||||||
if (storeFirstBlock && nextAddr<END_BOOT_BYTES+0x10) {
|
if (storeFirstBlock && nextAddr < END_BOOT_BYTES+0x10) {
|
||||||
storeFirstBlock[pos] = value;
|
storeFirstBlock[pos] = value;
|
||||||
}
|
}
|
||||||
checkSum += ((uint16_t)value) << ((pos&0x1)*8);
|
checkSum += ((uint16_t)value) << ((pos&0x1)*8);
|
||||||
@@ -707,7 +715,7 @@ bool flashPic(int fd) {
|
|||||||
// ih.verboseOn();
|
// ih.verboseOn();
|
||||||
// }
|
// }
|
||||||
inStream >> ih;
|
inStream >> ih;
|
||||||
if (ih.getNoErrors()>0 || ih.getNoWarnings()>0) {
|
if (ih.getNoErrors() > 0 || ih.getNoWarnings() > 0) {
|
||||||
std::cerr << "errors or warnings while reading the file:" << std::endl;
|
std::cerr << "errors or warnings while reading the file:" << std::endl;
|
||||||
string str;
|
string str;
|
||||||
while (ih.popNextWarning(str)) {
|
while (ih.popNextWarning(str)) {
|
||||||
@@ -730,7 +738,7 @@ bool flashPic(int fd) {
|
|||||||
<< std::hex << std::setfill('0') << std::setw(4) << static_cast<unsigned>(endAddr)
|
<< std::hex << std::setfill('0') << std::setw(4) << static_cast<unsigned>(endAddr)
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
}
|
}
|
||||||
if (startAddr<END_BOOT_BYTES || endAddr>=END_FLASH_BYTES || endAddr<startAddr || (startAddr&0xf) != 0) {
|
if (startAddr < END_BOOT_BYTES || endAddr >= END_FLASH_BYTES || endAddr < startAddr || (startAddr&0xf) != 0) {
|
||||||
std::cerr << "invalid address range" << std::endl;
|
std::cerr << "invalid address range" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -738,7 +746,8 @@ bool flashPic(int fd) {
|
|||||||
uint8_t buf[WRITE_FLASH_BLOCKSIZE];
|
uint8_t buf[WRITE_FLASH_BLOCKSIZE];
|
||||||
unsigned long nextAddr = ih.currentAddress();
|
unsigned long nextAddr = ih.currentAddress();
|
||||||
if (nextAddr != END_BOOT_BYTES) {
|
if (nextAddr != END_BOOT_BYTES) {
|
||||||
std::cerr << "unexpected start address in file: 0x" << std::hex << std::setfill('0') << std::setw(4) << static_cast<unsigned>(nextAddr) << std::endl;
|
std::cerr << "unexpected start address in file: 0x" << std::hex << std::setfill('0') << std::setw(4)
|
||||||
|
<< static_cast<unsigned>(nextAddr) << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
unsigned long blockStart = END_BOOT_BYTES;
|
unsigned long blockStart = END_BOOT_BYTES;
|
||||||
@@ -752,7 +761,7 @@ bool flashPic(int fd) {
|
|||||||
std::cout << "flashing: 0x" << std::hex << std::setfill('0') << std::setw(4) << static_cast<unsigned>(nextAddr/2)
|
std::cout << "flashing: 0x" << std::hex << std::setfill('0') << std::setw(4) << static_cast<unsigned>(nextAddr/2)
|
||||||
<< " - 0x" << static_cast<unsigned>(endAddr/2) << std::endl;
|
<< " - 0x" << static_cast<unsigned>(endAddr/2) << std::endl;
|
||||||
size_t blocks = 0;
|
size_t blocks = 0;
|
||||||
while (blockStart<endAddr) {
|
while (blockStart < endAddr) {
|
||||||
bool blank = true;
|
bool blank = true;
|
||||||
for (int pos = 0; pos < WRITE_FLASH_BLOCKSIZE; pos++, nextAddr++) {
|
for (int pos = 0; pos < WRITE_FLASH_BLOCKSIZE; pos++, nextAddr++) {
|
||||||
unsigned long addr = ih.currentAddress();
|
unsigned long addr = ih.currentAddress();
|
||||||
@@ -766,7 +775,8 @@ bool flashPic(int fd) {
|
|||||||
}
|
}
|
||||||
if (!blank) {
|
if (!blank) {
|
||||||
if (blocks == 0) {
|
if (blocks == 0) {
|
||||||
std::cout << std::endl << "0x" << std::hex << std::setfill('0') << std::setw(4) << static_cast<unsigned>(blockStart/2) << " ";
|
std::cout << std::endl << "0x" << std::hex << std::setfill('0') << std::setw(4)
|
||||||
|
<< static_cast<unsigned>(blockStart/2) << " ";
|
||||||
}
|
}
|
||||||
if (writeFlash(fd, blockStart/2, WRITE_FLASH_BLOCKSIZE, buf, true) != 0) {
|
if (writeFlash(fd, blockStart/2, WRITE_FLASH_BLOCKSIZE, buf, true) != 0) {
|
||||||
// repeat once silently:
|
// repeat once silently:
|
||||||
@@ -777,7 +787,7 @@ bool flashPic(int fd) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::cout << ".";
|
std::cout << ".";
|
||||||
if (++blocks>=64) {
|
if (++blocks >= 64) {
|
||||||
blocks = 0;
|
blocks = 0;
|
||||||
}
|
}
|
||||||
std::cout.flush();
|
std::cout.flush();
|
||||||
@@ -786,7 +796,7 @@ bool flashPic(int fd) {
|
|||||||
}
|
}
|
||||||
std::cout << std::endl << "flashing finished." << std::endl;
|
std::cout << std::endl << "flashing finished." << std::endl;
|
||||||
int picSum = calcChecksum(fd, startAddr/2, blockStart-startAddr);
|
int picSum = calcChecksum(fd, startAddr/2, blockStart-startAddr);
|
||||||
if (picSum<0) {
|
if (picSum < 0) {
|
||||||
std::cout << "unable to read checksum." << std::endl;
|
std::cout << "unable to read checksum." << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -807,9 +817,9 @@ void readIpSettings(int fd) {
|
|||||||
readConfig(fd, 0x0000, 8, false, false, configData); // User ID
|
readConfig(fd, 0x0000, 8, false, false, configData); // User ID
|
||||||
useMUI = (configData[1]&0x20) != 0; // if highest bit is set, then use MUI. if cleared, use User ID
|
useMUI = (configData[1]&0x20) != 0; // if highest bit is set, then use MUI. if cleared, use User ID
|
||||||
maskLen = configData[1]&0x1f;
|
maskLen = configData[1]&0x1f;
|
||||||
for (int i=0; i<4; i++) {
|
for (int i=0; i < 4; i++) {
|
||||||
ip[i] = configData[i*2];
|
ip[i] = configData[i*2];
|
||||||
if (!useMUI && i>0) {
|
if (!useMUI && i > 0) {
|
||||||
mac[2+i] = configData[i*2];
|
mac[2+i] = configData[i*2];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -817,12 +827,12 @@ void readIpSettings(int fd) {
|
|||||||
// read MUI to build uniqueMAC address
|
// read MUI to build uniqueMAC address
|
||||||
// start with MUI6, end with MUI8 (MUI9 is reserved)
|
// start with MUI6, end with MUI8 (MUI9 is reserved)
|
||||||
readConfig(fd, 0x0106, 8, true, false, configData); // MUI
|
readConfig(fd, 0x0106, 8, true, false, configData); // MUI
|
||||||
for (int i=0; i<3; i++) {
|
for (int i=0; i < 3; i++) {
|
||||||
mac[3+i] = configData[i*2];
|
mac[3+i] = configData[i*2];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::cout << "MAC address:";
|
std::cout << "MAC address:";
|
||||||
for (int i=0; i<6; i++) {
|
for (int i=0; i < 6; i++) {
|
||||||
std::cout << (i == 0?' ':':') << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned>(mac[i]);
|
std::cout << (i == 0?' ':':') << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned>(mac[i]);
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
@@ -830,20 +840,20 @@ void readIpSettings(int fd) {
|
|||||||
std::cout << "IP address: DHCP" << std::endl;
|
std::cout << "IP address: DHCP" << std::endl;
|
||||||
} else {
|
} else {
|
||||||
std::cout << "IP address:";
|
std::cout << "IP address:";
|
||||||
for (int i=0; i<4; i++) {
|
for (int i=0; i < 4; i++) {
|
||||||
std::cout << (i == 0?' ':'.') << std::dec << static_cast<unsigned>(ip[i]);
|
std::cout << (i == 0?' ':'.') << std::dec << static_cast<unsigned>(ip[i]);
|
||||||
}
|
}
|
||||||
std::cout << "/" << std::dec << static_cast<unsigned>(maskLen) << std::endl;
|
std::cout << "/" << std::dec << static_cast<unsigned>(maskLen) << std::endl;
|
||||||
/*
|
/*
|
||||||
// build gateway
|
// build gateway
|
||||||
for (uint8_t pos=0; pos<4; pos++) {
|
for (uint8_t pos=0; pos < 4; pos++) {
|
||||||
mask[pos] = maskLen>=8 ? 255 : maskLen<=0 ? 0 : (255^((1 << (8-maskLen))-1));
|
mask[pos] = maskLen >= 8 ? 255 : maskLen<=0 ? 0 : (255^((1 << (8-maskLen))-1));
|
||||||
ip[pos] &= mask[pos];
|
ip[pos] &= mask[pos];
|
||||||
maskLen = maskLen>=8 ? maskLen-8 : 0;
|
maskLen = maskLen >= 8 ? maskLen-8 : 0;
|
||||||
}
|
}
|
||||||
ip[3] |= 1; // first address in network is used as gateway (not needed anyway))
|
ip[3] |= 1; // first address in network is used as gateway (not needed anyway))
|
||||||
std::cout << "IP gateway:";
|
std::cout << "IP gateway:";
|
||||||
for (int i=0; i<4; i++) {
|
for (int i=0; i < 4; i++) {
|
||||||
std::cout << (i == 0?' ':'.') << std::dec << static_cast<unsigned>(ip[i]);
|
std::cout << (i == 0?' ':'.') << std::dec << static_cast<unsigned>(ip[i]);
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
@@ -885,7 +895,7 @@ int main(int argc, char* argv[]) {
|
|||||||
std::cerr << "incomplete IP arguments" << std::endl;
|
std::cerr << "incomplete IP arguments" << std::endl;
|
||||||
arg_index = argc; // force help output
|
arg_index = argc; // force help output
|
||||||
}
|
}
|
||||||
if (argc-arg_index<1) {
|
if (argc-arg_index < 1) {
|
||||||
if (flashFile) {
|
if (flashFile) {
|
||||||
printFileChecksum();
|
printFileChecksum();
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
@@ -896,7 +906,7 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int fd = openSerial(argv[arg_index]);
|
int fd = openSerial(argv[arg_index]);
|
||||||
if (fd<0) {
|
if (fd < 0) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user