source: rtos_arduino/trunk/arduino_lib/libraries/Bridge/src/YunClient.cpp@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 3.6 KB
Line 
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 <YunClient.h>
20
21YunClient::YunClient(int _h, BridgeClass &_b) :
22 bridge(_b), handle(_h), opened(true), buffered(0) {
23}
24
25YunClient::YunClient(BridgeClass &_b) :
26 bridge(_b), handle(0), opened(false), buffered(0) {
27}
28
29YunClient::~YunClient() {
30}
31
32YunClient& YunClient::operator=(const YunClient &_x) {
33 opened = _x.opened;
34 handle = _x.handle;
35 return *this;
36}
37
38void YunClient::stop() {
39 if (opened) {
40 uint8_t cmd[] = {'j', handle};
41 bridge.transfer(cmd, 2);
42 }
43 opened = false;
44}
45
46void YunClient::doBuffer() {
47 // If there are already char in buffer exit
48 if (buffered > 0)
49 return;
50
51 // Try to buffer up to 32 characters
52 readPos = 0;
53 uint8_t cmd[] = {'K', handle, sizeof(buffer)};
54 buffered = bridge.transfer(cmd, 3, buffer, sizeof(buffer));
55}
56
57int YunClient::available() {
58 // Look if there is new data available
59 doBuffer();
60 return buffered;
61}
62
63int YunClient::read() {
64 doBuffer();
65 if (buffered == 0)
66 return -1; // no chars available
67 else {
68 buffered--;
69 return buffer[readPos++];
70 }
71}
72
73int YunClient::read(uint8_t *buff, size_t size) {
74 int readed = 0;
75 do {
76 if (buffered == 0) {
77 doBuffer();
78 if (buffered == 0)
79 return readed;
80 }
81 buff[readed++] = buffer[readPos++];
82 buffered--;
83 } while (readed < size);
84 return readed;
85}
86
87int YunClient::peek() {
88 doBuffer();
89 if (buffered == 0)
90 return -1; // no chars available
91 else
92 return buffer[readPos];
93}
94
95size_t YunClient::write(uint8_t c) {
96 if (!opened)
97 return 0;
98 uint8_t cmd[] = {'l', handle, c};
99 bridge.transfer(cmd, 3);
100 return 1;
101}
102
103size_t YunClient::write(const uint8_t *buf, size_t size) {
104 if (!opened)
105 return 0;
106 uint8_t cmd[] = {'l', handle};
107 bridge.transfer(cmd, 2, buf, size, NULL, 0);
108 return size;
109}
110
111void YunClient::flush() {
112}
113
114uint8_t YunClient::connected() {
115 if (!opened)
116 return false;
117 uint8_t cmd[] = {'L', handle};
118 uint8_t res[1];
119 bridge.transfer(cmd, 2, res, 1);
120 return (res[0] == 1);
121}
122
123int YunClient::connect(IPAddress ip, uint16_t port) {
124 String address;
125 address.reserve(18);
126 address += ip[0];
127 address += '.';
128 address += ip[1];
129 address += '.';
130 address += ip[2];
131 address += '.';
132 address += ip[3];
133 return connect(address.c_str(), port);
134}
135
136int YunClient::connect(const char *host, uint16_t port) {
137 uint8_t tmp[] = {
138 'C',
139 (port >> 8) & 0xFF,
140 port & 0xFF
141 };
142 uint8_t res[1];
143 int l = bridge.transfer(tmp, 3, (const uint8_t *)host, strlen(host), res, 1);
144 if (l == 0)
145 return 0;
146 handle = res[0];
147
148 // wait for connection
149 uint8_t tmp2[] = { 'c', handle };
150 uint8_t res2[1];
151 while (true) {
152 bridge.transfer(tmp2, 2, res2, 1);
153 if (res2[0] == 0)
154 break;
155 delay(1);
156 }
157 opened = true;
158
159 // check for successful connection
160 if (connected())
161 return 1;
162
163 stop();
164 handle = 0;
165 return 0;
166}
167
Note: See TracBrowser for help on using the repository browser.