status_t UDF_ReadFromReader(Reader& origReader, UniversalDataFormat& out)
{
    BufferedReader reader(origReader, 1024);
    ulong_t lineNo = 0;
    ulong_t controlDataLength = 0;
    bool eof = false;
    out.reset();
    while (!eof)
    {
        NarrowString line;
        status_t error = reader.readLine(eof, line);
        if (errNone != error)
            return error;
            
        if (eof && line.empty()) //writer puts '\n' after last line... 
            break;

        error = parseUniversalDataFormatTextLine(line.data(), line.length(), out, lineNo, controlDataLength);
        if (errNone != error)
            return error;
    }
    assert(controlDataLength == out.dataLength());
    if (controlDataLength != out.dataLength())
        return SocketConnection::errResponseMalformed
        ;
    return errNone;
}
  NarrowString HTMLEntityCodec::encodeCharacter(const StringArray& immune, const NarrowString& ch) const
  {
    // ASSERT(!immune.empty());
    ASSERT(!ch.empty());

    if(ch.empty())
      return NarrowString();

    // check for immune characters
    StringArray::const_iterator it1 = std::find(immune.begin(), immune.end(), ch);
    if(it1 != immune.end())
      return ch;

    // check for simple alphanumeric characters
    if(ch.length() == 1 && ::isalnum(ch[0]))
      return ch;

    // check for illegal characterss
    //if ( ( c <= 0x1f && c != L'\t' && c != L'\n' && c != L'\r' ) || ( c >= 0x7f && c <= 0x9f ) )
    //{
    // hex = REPLACEMENT_HEX(); // Let's entity encode this instead of returning it
    // c = REPLACEMENT_CHAR();
    //}

    // check if there's a defined entity
    const EntityMap& map = getCharacterToEntityMap();
    EntityMapIterator it2 = map.find(ch);
    if(it2 != map.end())
      return String("&") + it2->second + String(";");

    // return the hex entity as suggested in the spec
    return NarrowString("&#x") + toHex(ch) + NarrowString(";");
  }
Beispiel #3
0
static status_t validateAddress(const NarrowString& origAddress, NarrowString& validAddress, ushort_t& port)
{
    NarrowString::size_type pos = origAddress.find(':', 1);
    if (origAddress.npos == pos)
        return netErrParamErr;
    
    ushort_t portLength = origAddress.length() - pos - 1;
    if (portLength>0)
    {
        long value=0;
        status_t error = numericValue(origAddress.data()+pos+1, origAddress.data()+pos+1+portLength, value);
        if ((errNone != error) || (value > ushort_t(-1)))
            return netErrParamErr;

        port = ushort_t(value);
    }

    if (0 == port)
        return netErrParamErr;        

    validAddress.assign(origAddress, 0, pos);
    return errNone;
}
Beispiel #4
0
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--;
    }
  }
}
Beispiel #5
0
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;
}