aboutsummaryrefslogtreecommitdiff
path: root/libraries/Bridge/Mailbox.cpp
blob: cf2b9e50457c6690b551016c61fa63b859f322b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
  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 <Mailbox.h>

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

void MailboxClass::readMessage(String &str, unsigned int maxLength) {
  uint8_t tmp[] = { 'm' };
  // XXX: Is there a better way to create the string?
  uint8_t buff[maxLength+1];
  int l = bridge.transfer(tmp, 1, buff, maxLength);
  buff[l] = 0;
  str = (const char *)buff;
}

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

void MailboxClass::writeMessage(const String& str) {
  writeMessage((uint8_t*) str.c_str(), str.length());
}

void MailboxClass::writeJSON(const String& str) {
  uint8_t cmd[] = {'J'};
  bridge.transfer(cmd, 1, (uint8_t*) str.c_str(), str.length(), NULL, 0);
}

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

MailboxClass Mailbox(Bridge);