libraries / Bridge / src / BridgeServer.cppon commit Added link to project report (97a3ba0)
   1/*
   2  Copyright (c) 2013 Arduino LLC. All right reserved.
   3
   4  This library is free software; you can redistribute it and/or
   5  modify it under the terms of the GNU Lesser General Public
   6  License as published by the Free Software Foundation; either
   7  version 2.1 of the License, or (at your option) any later version.
   8
   9  This library is distributed in the hope that it will be useful,
  10  but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12  Lesser General Public License for more details.
  13
  14  You should have received a copy of the GNU Lesser General Public
  15  License along with this library; if not, write to the Free Software
  16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17*/
  18
  19#include <BridgeServer.h>
  20#include <BridgeClient.h>
  21
  22BridgeServer::BridgeServer(uint16_t _p, BridgeClass &_b) :
  23  bridge(_b), port(_p), listening(false), useLocalhost(false) {
  24}
  25
  26void BridgeServer::begin() {
  27  uint8_t tmp[] = {
  28    'N',
  29    static_cast<uint8_t>(port >> 8),
  30    static_cast<uint8_t>(port)
  31  };
  32  uint8_t res[1];
  33  String address = F("127.0.0.1");
  34  if (!useLocalhost)
  35    address = F("0.0.0.0");
  36  bridge.transfer(tmp, 3, (const uint8_t *)address.c_str(), address.length(), res, 1);
  37  listening = (res[0] == 1);
  38}
  39
  40BridgeClient BridgeServer::accept() {
  41  uint8_t cmd[] = {'k'};
  42  uint8_t res[1];
  43  unsigned int l = bridge.transfer(cmd, 1, res, 1);
  44  if (l == 0)
  45    return BridgeClient();
  46  return BridgeClient(res[0]);
  47}
  48
  49size_t BridgeServer::write(uint8_t c) {
  50  uint8_t cmd[] = { 'b', c };
  51  bridge.transfer(cmd, 2);
  52  return 1;
  53}
  54