static inline void HexDumpLine(const char *prefix, unsigned offset, const uint8_t *data, size_t length) { NarrowString<128> line; line.clear(); for (size_t i = 0; i < length; ++i) { if ((i & 0x7) == 0) line += " "; line.AppendFormat(" %02x", data[i]); } for (size_t i = length; i < 0x10; ++i) { if ((i & 0x7) == 0) line += " "; line += " "; } line += " "; for (size_t i = 0; i < length; ++i) { if ((i & 0x7) == 0) line += " "; char byte[2]; byte[0] = IsPrintable(data[i]) ? (char)data[i] : '.'; byte[1] = '\0'; line += byte; } LogFormat("%s%04x%s", prefix, offset, line.c_str()); }
bool TCPClientPort::Connect(const char *host, unsigned port) { NarrowString<32> service; service.UnsafeFormat("%u", port); resolver.async_resolve({host, service.c_str()}, std::bind(&TCPClientPort::OnResolved, this, std::placeholders::_1, std::placeholders::_2)); return true; }
bool LX16xxConfigWidget::SaveUnsignedSetting(LX1600::Setting key, unsigned idx, LX1600::SettingsMap &settings) { const std::string old_value = device.GetLX16xxSetting(key); unsigned value = ParseUnsigned(old_value.c_str()); if (!SaveValue(idx, value)) return false; NarrowString<32> buffer; buffer.UnsafeFormat("%u", value); settings[key] = std::string(buffer.c_str(), buffer.end()); return true; }
static status_t blockingResolve(SocketAddress& out, NetLibrary& netLib, const NarrowString& name, ushort_t port, ulong_t timeout) { std::auto_ptr<HostInfoBuffer> buffer(new HostInfoBuffer); memzero(buffer.get(), sizeof(HostInfoBuffer)); assert(!netLib.closed()); status_t error=netLib.getHostByName(name.c_str(), *buffer, timeout); if (error) return error; IPAddr resAddr=buffer->getAddress(); assert(resAddr.ip != 0); INetSocketAddress addr(resAddr, port); out=addr; return errNone; }
bool LX16xxConfigWidget::SaveFixedEnumSetting(LX1600::Setting key, unsigned idx, LX1600::SettingsMap &settings, unsigned factor) { const std::string old_value = device.GetLX16xxSetting(key); unsigned value = unsigned(ParseDouble(old_value.c_str()) * factor); if (!SaveValue(idx, value)) return false; NarrowString<32> buffer; buffer.UnsafeFormat("%.2f", (double)value / factor); settings[key] = std::string(buffer.c_str(), buffer.end()); return true; }
status_t resolve(SocketAddress& out, NetLibrary& netLib, const char* address, ushort_t port, ulong_t timeout) { NarrowString validAddress; status_t error = validateAddress(address, validAddress, port); if (error) return error; INetSocketAddress addr; // TODO: the assumption that the ip address is numeric if the first // letter is a digit is incorrect. A symbolic name can also start // with a digit. Better test would be to check if the whole name // consists of digits or "." if (!validAddress.empty() && isDigit(validAddress[0])) { error=netLib.addrAToIN(validAddress.c_str(), addr); if (error) return error; addr.setPort(port); out=addr; return errNone; } return blockingResolve(out, netLib, validAddress, port, timeout); }
int main(int argc, char **argv) { Args args(argc, argv, "PORT"); const DeviceConfig config = ParsePortArgs(args); args.ExpectEnd(); InitialiseIOThread(); Port *port = OpenPort(config, nullptr, *(DataHandler *)nullptr); if (port == nullptr) { DeinitialiseIOThread(); fprintf(stderr, "Failed to open port\n"); return EXIT_FAILURE; } ConsoleOperationEnvironment env; if (!port->WaitConnected(env)) { delete port; DeinitialiseIOThread(); fprintf(stderr, "Failed to connect the port\n"); return EXIT_FAILURE; } PeriodClock start_clock; start_clock.Update(); PeriodClock pressure_clock; PeriodClock battery_clock; fixed pressure = fixed(101300); unsigned battery_level = 11; while (true) { if (pressure_clock.CheckUpdate(48)) { NarrowString<16> sentence; int elapsed_ms = start_clock.Elapsed(); auto elapsed = fixed(elapsed_ms) / 1000; auto vario = sin(elapsed / 3) * cos(elapsed / 10) * cos(elapsed / 20 + fixed(2)) * 3; auto pressure_vario = -vario * fixed(12.5); auto delta_pressure = pressure_vario * 48 / 1000; pressure += delta_pressure; sentence = "_PRS "; sentence.AppendFormat("%08X", uround(pressure)); sentence += "\n"; port->Write(sentence.c_str(), sentence.length()); } if (battery_clock.CheckUpdate(11000)) { NarrowString<16> sentence; sentence = "_BAT "; if (battery_level <= 10) sentence.AppendFormat("%X", battery_level); else sentence += "*"; sentence += "\n"; port->Write(sentence.c_str(), sentence.length()); if (battery_level == 0) battery_level = 11; else battery_level--; } } }
int main(int argc, char **argv) { // Determine on which TCP port to connect to the server const char *tcp_port; if (argc < 2) { fprintf(stderr, "This program opens a TCP connection to a server which is assumed "); fprintf(stderr, "to be at 127.0.0.1, and sends artificial FlyNet vario data.\n\n"); fprintf(stderr, "Usage: %s PORT\n", argv[0]); fprintf(stderr, "Defaulting to port 4353\n"); tcp_port = "4353"; } else { tcp_port = argv[1]; } // Convert IP address to binary form SocketAddress server_address; if (!server_address.Lookup("127.0.0.1", tcp_port, AF_INET)) { fprintf(stderr, "Failed to look up address\n"); exit(EXIT_FAILURE); } // Create socket for the outgoing connection SocketDescriptor sock; if (!sock.CreateTCP()) { perror("Socket"); exit(EXIT_FAILURE); } // Connect to the specified server if (!sock.Connect(server_address)) { perror("Connect"); exit(EXIT_FAILURE); } PeriodClock start_clock; start_clock.Update(); PeriodClock pressure_clock; PeriodClock battery_clock; fixed pressure = fixed(101300); unsigned battery_level = 11; while (true) { if (pressure_clock.CheckUpdate(48)) { NarrowString<16> sentence; int elapsed_ms = start_clock.Elapsed(); fixed elapsed = fixed(elapsed_ms) / 1000; fixed vario = sin(elapsed / 3) * cos(elapsed / 10) * cos(elapsed / 20 + fixed(2)) * fixed(3); fixed pressure_vario = -vario * fixed(12.5); fixed delta_pressure = pressure_vario * 48 / 1000; pressure += delta_pressure; sentence = "_PRS "; sentence.AppendFormat("%08X", uround(pressure)); sentence += "\n"; sock.Write(sentence.c_str(), sentence.length()); } if (battery_clock.CheckUpdate(11000)) { NarrowString<16> sentence; sentence = "_BAT "; if (battery_level <= 10) sentence.AppendFormat("%X", battery_level); else sentence += "*"; sentence += "\n"; sock.Write(sentence.c_str(), sentence.length()); if (battery_level == 0) battery_level = 11; else battery_level--; } } return EXIT_SUCCESS; }
NarrowString toLower (const NarrowString & nstr) { NarrowString retval (nstr.c_str()); std::transform (retval.begin (), retval.end (), retval.begin (), trueLower); return retval; }