|
| 1 | +/* |
| 2 | + This file is part of the ArduinoBLE library. |
| 3 | + Copyright (c) 2018 Arduino SA. All rights reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +#if defined(ARDUINO_UNOR4_WIFI) |
| 21 | + |
| 22 | +#include "HCIVirtualTransportAT.h" |
| 23 | + |
| 24 | +extern ModemClass modem; |
| 25 | + |
| 26 | +HCIVirtualTransportATClass::HCIVirtualTransportATClass() |
| 27 | +{ |
| 28 | +} |
| 29 | + |
| 30 | +HCIVirtualTransportATClass::~HCIVirtualTransportATClass() |
| 31 | +{ |
| 32 | +} |
| 33 | + |
| 34 | +static RingBufferN<258> buf; |
| 35 | + |
| 36 | +int HCIVirtualTransportATClass::begin() |
| 37 | +{ |
| 38 | + // TODO: add this helper |
| 39 | + //modem.debug(Serial); |
| 40 | + buf.clear(); |
| 41 | + //modem.debug(true); |
| 42 | + std::string res = ""; |
| 43 | + modem.begin(); |
| 44 | + if (modem.write(std::string(PROMPT(_HCI_BEGIN)), res, CMD(_HCI_BEGIN))) { |
| 45 | + return 1; |
| 46 | + } |
| 47 | + return 0; |
| 48 | +} |
| 49 | + |
| 50 | +void HCIVirtualTransportATClass::end() |
| 51 | +{ |
| 52 | +} |
| 53 | + |
| 54 | +void HCIVirtualTransportATClass::wait(unsigned long timeout) |
| 55 | +{ |
| 56 | + std::string res = ""; |
| 57 | + modem.write(std::string(PROMPT(_HCI_WAIT)), res, "%d\n\r", CMD_WRITE(_HCI_WAIT), timeout); |
| 58 | +} |
| 59 | + |
| 60 | +int HCIVirtualTransportATClass::available() |
| 61 | +{ |
| 62 | + std::string res = ""; |
| 63 | + if (buf.available()) { |
| 64 | + return buf.available(); |
| 65 | + } |
| 66 | + if (modem.write(std::string(PROMPT(_HCI_AVAILABLE)), res, CMD_READ(_HCI_AVAILABLE))) { |
| 67 | + return atoi(res.c_str()); |
| 68 | + } |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
| 72 | + |
| 73 | +// never called |
| 74 | +int HCIVirtualTransportATClass::peek() |
| 75 | +{ |
| 76 | + return -1; |
| 77 | +} |
| 78 | + |
| 79 | +int HCIVirtualTransportATClass::read() |
| 80 | +{ |
| 81 | + uint8_t c; |
| 82 | + std::string res = ""; |
| 83 | + if (buf.available()) { |
| 84 | + return buf.read_char(); |
| 85 | + } |
| 86 | + modem.avoid_trim_results(); |
| 87 | + modem.read_using_size(); |
| 88 | + if (modem.write(std::string(PROMPT(_HCI_READ)), res, CMD(_HCI_READ))) { |
| 89 | + for(int i = 0; i < res.size(); i++) { |
| 90 | + buf.store_char((uint8_t)res[i]); |
| 91 | + } |
| 92 | + return buf.read_char(); |
| 93 | + } |
| 94 | + |
| 95 | + return -1; |
| 96 | +} |
| 97 | + |
| 98 | +size_t HCIVirtualTransportATClass::write(const uint8_t* data, size_t length) |
| 99 | +{ |
| 100 | + std::string res = ""; |
| 101 | + modem.write_nowait(std::string(PROMPT(_HCI_WRITE)), res, "%s%d\r\n" , CMD_WRITE(_HCI_WRITE), length); |
| 102 | + if(modem.passthrough(data, length)) { |
| 103 | + return length; |
| 104 | + } |
| 105 | + return 0; |
| 106 | +} |
| 107 | + |
| 108 | +HCIVirtualTransportATClass HCIVirtualTransportAT; |
| 109 | + |
| 110 | +HCITransportInterface& HCITransport = HCIVirtualTransportAT; |
| 111 | + |
| 112 | +#endif |
0 commit comments