<commit_before>/*
MQTT.cpp - MQTT packet classes
Copyright (C) 2015 <NAME>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "MQTT.h"

namespace MQTT {
  // First some convenience functions
  void write(uint8_t *buf, uint8_t& bufpos, uint16_t data) {
    buf[bufpos++] = data >> 8;
    buf[bufpos++] = data & 0xff;
  }

  void write(uint8_t *buf, uint8_t& bufpos, uint8_t *data, uint8_t dlen) {
    memcpy(buf + bufpos, data, dlen);
    bufpos += dlen;
  }

  void write(uint8_t *buf, uint8_t& bufpos, String str) {
    const char* c = str.c_str();
    uint8_t length_pos = bufpos;
    bufpos += 2;
    uint16_t count = 0;
    while (*c) {
      buf[bufpos++] = *c++;
      count++;
    }
    write(buf, length_pos, count);
  }

  template <typename T>
  T read(uint8_t *buf, uint8_t& pos);

  template <>
  uint8_t read<uint8_t>(uint8_t *buf, uint8_t& pos) {
    return buf[pos++];
  }

  template <>
  uint16_t read<uint16_t>(uint8_t *buf, uint8_t& pos) {
    uint16_t val = buf[pos++] << 8;
    val |= buf[pos++];
    return val;
  }

  template <>
  String read<String>(uint8_t *buf, uint8_t& pos) {
    uint16_t len = read<uint16_t>(buf, pos);
    String val;
    val.reserve(len);
    for (uint8_t i = 0; i < len; i++)
      val += (char)read<uint8_t>(buf, pos);

    return val;
  }


  // Message class
  bool Message::write_fixed_header(uint8_t *buf, uint8_t& bufpos, uint8_t rlength) {
    buf[bufpos] = _type << 4;

    switch (_type) {
    case MQTTPUBLISH:
      buf[bufpos] |= _flags & 0x0f;
      break;
    case MQTTPUBREL:
    case MQTTSUBSCRIBE:
    case MQTTUNSUBSCRIBE:
      buf[bufpos] |= 0x02;
    }
    bufpos++;

    // Remaning length
    do {
      uint8_t digit = rlength & 0x7f;
      rlength >>= 7;
      if (rlength)
	digit |= 0x80;
      buf[bufpos++] = digit;
    } while (rlength);

    return true;
  }

  bool Message::write_packet_id(uint8_t *buf, uint8_t& bufpos) {
    write(buf, bufpos, _packet_id);
  }

  bool Message::send(WiFiClient& wclient) {
    uint8_t packet[MQTT_MAX_PACKET_SIZE];
    uint8_t remaining_length = 0;
    write_variable_header(packet + 5, remaining_length);
    write_payload(packet + 5, remaining_length);

    uint8_t fixed_header[5], fixed_len = 0;
    write_fixed_header(fixed_header, fixed_len, remaining_length);

    uint8_t *real_packet = packet + 5 - fixed_len;
    uint8_t real_len = remaining_length + fixed_len;
    memcpy(real_packet, fixed_header, fixed_len);

    uint8_t sent = wclient.write(real_packet, real_len);
    return (sent == real_len);
  }


  uint8_t readByte(WiFiClient& client) {
    while(!client.available()) {}
    return client.read();
  }

  // Parser<commit_msg>Be more precise with pointer type passed to WiFiClient.write() now that there are template overloaded versions<commit_after>/*
MQTT.cpp - MQTT packet classes
Copyright (C) 2015 <NAME>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "MQTT.h"

namespace MQTT {
  // First some convenience functions
  void write(uint8_t *buf, uint8_t& bufpos, uint16_t data) {
    buf[bufpos++] = data >> 8;
    buf[bufpos++] = data & 0xff;
  }

  void write(uint8_t *buf, uint8_t& bufpos, uint8_t *data, uint8_t dlen) {
    memcpy(buf + bufpos, data, dlen);
    bufpos += dlen;
  }

  void write(uint8_t *buf, uint8_t& bufpos, String str) {
    const char* c = str.c_str();
    uint8_t length_pos = bufpos;
    bufpos += 2;
    uint16_t count = 0;
    while (*c) {
      buf[bufpos++] = *c++;
      count++;
    }
    write(buf, length_pos, count);
  }

  template <typename T>
  T read(uint8_t *buf, uint8_t& pos);

  template <>
  uint8_t read<uint8_t>(uint8_t *buf, uint8_t& pos) {
    return buf[pos++];
  }

  template <>
  uint16_t read<uint16_t>(uint8_t *buf, uint8_t& pos) {
    uint16_t val = buf[pos++] << 8;
    val |= buf[pos++];
    return val;
  }

  template <>
  String read<String>(uint8_t *buf, uint8_t& pos) {
    uint16_t len = read<uint16_t>(buf, pos);
    String val;
    val.reserve(len);
    for (uint8_t i = 0; i < len; i++)
      val += (char)read<uint8_t>(buf, pos);

    return val;
  }


  // Message class
  bool Message::write_fixed_header(uint8_t *buf, uint8_t& bufpos, uint8_t rlength) {
    buf[bufpos] = _type << 4;

    switch (_type) {
    case MQTTPUBLISH:
      buf[bufpos] |= _flags & 0x0f;
      break;
    case MQTTPUBREL:
    case MQTTSUBSCRIBE:
    case MQTTUNSUBSCRIBE:
      buf[bufpos] |= 0x02;
    }
    bufpos++;

    // Remaning length
    do {
      uint8_t digit = rlength & 0x7f;
      rlength >>= 7;
      if (rlength)
	digit |= 0x80;
      buf[bufpos++] = digit;
    } while (rlength);

    return true;
  }

  bool Message::write_packet_id(uint8_t *buf, uint8_t& bufpos) {
    write(buf, bufpos, _packet_id);
  }

  bool Message::send(WiFiClient& wclient) {
    uint8_t packet[MQTT_MAX_PACKET_SIZE];
    uint8_t remaining_length = 0;
    write_variable_header(packet + 5, remaining_length);
    write_payload(packet + 5, remaining_length);

    uint8_t fixed_header[5], fixed_len = 0;
    write_fixed_header(fixed_header, fixed_len, remaining_length);

    uint8_t *real_packet = packet + 5 - fixed_len;
    uint8_t real_len = remaining_length + fixed_len;
    memcpy(real_packet, fixed_header, fixed_len);

    uint8_t sent = wclient.write(const_cast<const uint8_t*>(real_packet), real_len);
    return (sent == real_len);
  }


  uint8_t readByte(WiFiClient& client) {
    while(!client.available()) {}
    return client.read();
  }

  // Parser
{'fraction_non_alphanumeric': 0.11171702284450995, 'fraction_numerical': 0.026381724392041266, 'mean_word_length': 3.3222929936305734, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2427950', 'n_tokens_mistral': 2522, 'n_tokens_neox': 2384, 'n_words': 707}