<commit_before>/*
  Copyright (c) 2013 Arduino LLC. All right reserved.

  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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "Bridge.h"

BridgeClass::BridgeClass(Stream &_stream) : index(0), stream(_stream), started(false) {
  // Empty
}

void BridgeClass::begin() {
  if (started)
    return;
  started = true;
  
  // TODO: A more robust restart
  
  // Wait for Atheros bootloader to finish startup
  do {
    dropAll();
    delay(1100);
  } while (available()>0);
  
  // Bridge startup:
  // - If the bridge is not running starts it safely
  print(CTRL_C);
  print(F("\n"));
  delay(500);
  // Wait for OpenWRT message
  // "Press enter to activate console"
  print(F("run-bridge\n"));
  delay(1000);
  dropAll();
  
  // - If the bridge was already running previous commands
  //   are ignored as "invalid packets".
  
  // Reset the brigde
  uint8_t cmd[] = {'X','X'};
  transfer(cmd, 2);
}

uint8_t BridgeClass::runCommand(String &command) {
  // TODO: do it in a more efficient way
  String cmd = "R" + command;
  uint8_t res[1];
  transfer((uint8_t*)cmd.c_str(), cmd.length(), res, 1);
  return res[0];
}

bool BridgeClass::commandIsRunning(uint8_t handle) {
  uint8_t cmd[] = {'r', handle};
  uint8_t res[1];
  transfer(cmd, 2, res, 1);
  return (res[0] == 1);
}

unsigned int BridgeClass::commandExitValue(uint8_t handle) {
  uint8_t cmd[] = {'W', handle};
  uint8_t res[2];
  transfer(cmd, 2, res, 2);
  return (res[0] << 8) + res[1];
}

void BridgeClass::cleanCommand(uint8_t handle) {
  uint8_t cmd[] = {'w', handle};
  transfer(cmd, 2);
}

unsigned int BridgeClass::commandOutputAvailable(uint8_t handle) {
  uint8_t cmd[] = {'o', handle};
  uint8_t res[1];
  transfer(cmd, 2, res, 1);
  return res[0];
}

unsigned int BridgeClass::readCommandOutput(uint8_t handle, 
                                            uint8_t *buffer, unsigned int size) {
  if (size > 255)
    size = 255;
  uint8_t cmd[] = {'O', handle, size};
  return transfer(cmd, 3, buffer, size);
}

void BridgeClass::writeCommandInput(uint8_t handle, 
                                    const uint8_t *buff, unsigned int size) {
  // TODO: do it in a more efficient way
  uint8_t *tmp = new uint8_t[size+2];
  tmp[0] = 'I';
  tmp[1] = handle;
  memcpy(tmp+2, buff, size);
  transfer(tmp, size+2);
  delete[] tmp;
}

unsigned int BridgeClass::readMessage(uint8_t *buff, unsigned int size) {
  uint8_t tmp[] = { 'm' };
  return transfer(tmp, 1, buff, size);
}

void BridgeClass::writeMessage(const uint8_t *buff, unsigned int size) {
  // TODO: do it in a more efficient way
  uint8_t *tmp = new uint8_t[size+1];
  tmp[0] = 'M';
  memcpy(tmp+1, buff, size);
  transfer(tmp, size+1);
  delete[] tmp;
}

unsigned int BridgeClass::messageAvailable() {
  uint8_t tmp[] = { 'n' };
  uint8_t res[2];
  transfer(tmp, 1, res, 2);
  return (res[0] << 8) + res[1];
}

void BridgeClass::put(const char *key, const char *value) {
  // TODO: do it in a more efficient way
  String cmd = "D";
  cmd += key;
  cmd += "\xFE";
  cmd += value;
  transfer((uint8_t*)cmd.c_str(), cmd.length());
}

unsigned int BridgeClass::get(const char *key, uint8_t *value, unsigned int maxlen) {
  // TODO: do it in a more efficient way
  unsigned int l = strlen(key);
  uint8_t *tmp = new uint8_t[l+1];
  tmp[0] = 'd';
  memcpy(tmp+1, key, strlen(key));
  l = transfer(tmp, l+1, value, maxlen);
  if (l<maxlen)
    value[l] = 0; // Zero-terminate string
  delete[] tmp;
  return l;
}

void BridgeClass::crcUpdate(uint8_t c) {
  CRC = CRC ^ c;
  CRC = (CRC >> 8) + (CRC << 8);
}

void BridgeClass::crcReset() {
  CRC = 0xAAAA;
}

void BridgeClass::crcWrite() {
  write((char)(CRC >> 8));
  write((char)(CRC & 0xFF));
}

bool BridgeClass::crcCheck(uint16_t _CRC) {
  return CRC == _CRC;
}

uint8_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1,
                 const uint8_t *buff2, uint16_t len2,
                 const uint8_t *buff3, uint16_t len3,
                 uint8_t *rxbuff, uint16_t rxlen)
{
  uint16_t len = len1 + len2 + len3;
  for ( ; ; delay(100), dropAll() /* Delay for retransmission */) {
    // Send packet
    crcReset();
    write((char)0xFF);                // Start of packet (0xFF)
    crcUpdate(0xFF);
    write((char)index);               // Message index
    crcUpdate(index);
    write((char)((len >> 8) & 0xFF)); // Message length (hi)
    crcUpdate((len >> 8) & 0xFF);
    write((char)(len & 0xFF));        // Message length (lo)
    crcUpdate(len & 0xFF);
    for (uint8_t i=0; i<len1; i++) {  // Payload
      write((char)buff1[i]);
      crcUpdate(buff1[i]);
    }
    for (uint8_t i=0; i<len2; i++) {  // Payload
      write((char)buff2[i]);
      crcUpdate(buff2[i]);
    }
    for (uint8_t i=0; i<len3; i++) {  // Payload
      write((char)buff3[i]);
      crcUpdate(buff3[i]);
    }<commit_msg>Improved Bridge memory usage. Fixed bug affecting buffers with more than 255 bytes<commit_after>/*
  Copyright (c) 2013 Arduino LLC. All right reserved.

  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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "Bridge.h"

BridgeClass::BridgeClass(Stream &_stream) : index(0), stream(_stream), started(false) {
  // Empty
}

void BridgeClass::begin() {
  if (started)
    return;
  started = true;
  
  // TODO: A more robust restart
  
  // Wait for Atheros bootloader to finish startup
  do {
    dropAll();
    delay(1100);
  } while (available()>0);
  
  // Bridge startup:
  // - If the bridge is not running starts it safely
  print(CTRL_C);
  print(F("\n"));
  delay(500);
  // Wait for OpenWRT message
  // "Press enter to activate console"
  print(F("run-bridge\n"));
  delay(1000);
  dropAll();
  
  // - If the bridge was already running previous commands
  //   are ignored as "invalid packets".
  
  // Reset the brigde
  uint8_t cmd[] = {'X','X'};
  transfer(cmd, 2);
}

uint8_t BridgeClass::runCommand(String &command) {
  uint8_t cmd[] = {'R'};
  uint8_t res[1];
  transfer(cmd, 1, (uint8_t*)command.c_str(), command.length(), res, 1);
  return res[0];
}

bool BridgeClass::commandIsRunning(uint8_t handle) {
  uint8_t cmd[] = {'r', handle};
  uint8_t res[1];
  transfer(cmd, 2, res, 1);
  return (res[0] == 1);
}

unsigned int BridgeClass::commandExitValue(uint8_t handle) {
  uint8_t cmd[] = {'W', handle};
  uint8_t res[2];
  transfer(cmd, 2, res, 2);
  return (res[0] << 8) + res[1];
}

void BridgeClass::cleanCommand(uint8_t handle) {
  uint8_t cmd[] = {'w', handle};
  transfer(cmd, 2);
}

unsigned int BridgeClass::commandOutputAvailable(uint8_t handle) {
  uint8_t cmd[] = {'o', handle};
  uint8_t res[1];
  transfer(cmd, 2, res, 1);
  return res[0];
}

unsigned int BridgeClass::readCommandOutput(uint8_t handle, 
                                            uint8_t *buffer, unsigned int size) {
  if (size > 255)
    size = 255;
  uint8_t cmd[] = {'O', handle, size};
  return transfer(cmd, 3, buffer, size);
}

void BridgeClass::writeCommandInput(uint8_t handle, 
                                    const uint8_t *buff, unsigned int size) {
  uint8_t cmd[] = {'I', handle};
  transfer(cmd, 2, buff, size, NULL, 0);
}

unsigned int BridgeClass::readMessage(uint8_t *buff, unsigned int size) {
  uint8_t tmp[] = { 'm' };
  return transfer(tmp, 1, buff, size);
}

void BridgeClass::writeMessage(const uint8_t *buff, unsigned int size) {
  uint8_t cmd[] = {'M'};
  transfer(cmd, 1, buff, size, NULL, 0);
}

unsigned int BridgeClass::messageAvailable() {
  uint8_t tmp[] = {'n'};
  uint8_t res[2];
  transfer(tmp, 1, res, 2);
  return (res[0] << 8) + res[1];
}

void BridgeClass::put(const char *key, const char *value) {
  // TODO: do it in a more efficient way
  String cmd = "D";
  cmd += key;
  cmd += "\xFE";
  cmd += value;
  transfer((uint8_t*)cmd.c_str(), cmd.length());
}

unsigned int BridgeClass::get(const char *key, uint8_t *value, unsigned int maxlen) {
  uint8_t cmd[] = {'d'};
  unsigned int l = transfer(cmd, 1, (uint8_t *)key, strlen(key), value, maxlen);
  if (l < maxlen)
    value[l] = 0; // Zero-terminate string
  return l;
}

void BridgeClass::crcUpdate(uint8_t c) {
  CRC = CRC ^ c;
  CRC = (CRC >> 8) + (CRC << 8);
}

void BridgeClass::crcReset() {
  CRC = 0xAAAA;
}

void BridgeClass::crcWrite() {
  write((char)(CRC >> 8));
  write((char)(CRC & 0xFF));
}

bool BridgeClass::crcCheck(uint16_t _CRC) {
  return CRC == _CRC;
}

uint8_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1,
                 const uint8_t *buff2, uint16_t len2,
                 const uint8_t *buff3, uint16_t len3,
                 uint8_t *rxbuff, uint16_t rxlen)
{
  uint16_t len = len1 + len2 + len3;
  for ( ; ; delay(100), dropAll() /* Delay for retransmission */) {
    // Send packet
    crcReset();
    write((char)0xFF);                // Start of packet (0xFF)
    crcUpdate(0xFF);
    write((char)index);               // Message index
    crcUpdate(index);
    write((char)((len >> 8) & 0xFF)); // Message length (hi)
    crcUpdate((len >> 8) & 0xFF);
    write((char)(len & 0xFF));        // Message length (lo)
    crcUpdate(len & 0xFF);
    for (uint16_t i=0; i<len1; i++) {  // Payload
      write((char)buff1[i]);
      crcUpdate(buff1[i]);
    }
    for (uint16_t i=0; i<len2; i++) {  // Payload
      write((char)buff2[i]);
      crcUpdate(buff2[i]);
    }
    for (uint16_t i=0; i<len3; i++) {  // Payload
      write((char)buff3[i]);
      crcUpdate(buff3[i]);
    }
{'fraction_non_alphanumeric': 0.14485149442518505, 'fraction_numerical': 0.031668696711327646, 'mean_word_length': 3.174423152131404, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7143095', 'n_tokens_mistral': 3873, 'n_tokens_neox': 3701, 'n_words': 1134}