source: rtos_arduino/trunk/arduino_lib/libraries/pubsubclient-2.6/tests/src/lib/ShimClient.cpp@ 209

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

BlueMix用のフィアルを追加

File size: 3.9 KB
Line 
1#include "ShimClient.h"
2#include "trace.h"
3#include <iostream>
4#include <Arduino.h>
5#include <ctime>
6
7extern "C" {
8 uint32_t millis(void) {
9 return time(0)*1000;
10 }
11}
12
13ShimClient::ShimClient() {
14 this->responseBuffer = new Buffer();
15 this->expectBuffer = new Buffer();
16 this->_allowConnect = true;
17 this->_connected = false;
18 this->_error = false;
19 this->expectAnything = true;
20 this->_received = 0;
21 this->_expectedPort = 0;
22}
23
24int ShimClient::connect(IPAddress ip, uint16_t port) {
25 if (this->_allowConnect) {
26 this->_connected = true;
27 }
28 if (this->_expectedPort !=0) {
29 // if (memcmp(ip,this->_expectedIP,4) != 0) {
30 // TRACE( "ip mismatch\n");
31 // this->_error = true;
32 // }
33 if (port != this->_expectedPort) {
34 TRACE( "port mismatch\n");
35 this->_error = true;
36 }
37 }
38 return this->_connected;
39}
40int ShimClient::connect(const char *host, uint16_t port) {
41 if (this->_allowConnect) {
42 this->_connected = true;
43 }
44 if (this->_expectedPort !=0) {
45 if (strcmp(host,this->_expectedHost) != 0) {
46 TRACE( "host mismatch\n");
47 this->_error = true;
48 }
49 if (port != this->_expectedPort) {
50 TRACE( "port mismatch\n");
51 this->_error = true;
52 }
53
54 }
55 return this->_connected;
56}
57size_t ShimClient::write(uint8_t b) {
58 this->_received += 1;
59 TRACE(std::hex << (unsigned int)b);
60 if (!this->expectAnything) {
61 if (this->expectBuffer->available()) {
62 uint8_t expected = this->expectBuffer->next();
63 if (expected != b) {
64 this->_error = true;
65 TRACE("!=" << (unsigned int)expected);
66 }
67 } else {
68 this->_error = true;
69 }
70 }
71 TRACE("\n"<< std::dec);
72 return 1;
73}
74size_t ShimClient::write(const uint8_t *buf, size_t size) {
75 this->_received += size;
76 TRACE( "[" << std::dec << (unsigned int)(size) << "] ");
77 uint16_t i=0;
78 for (;i<size;i++) {
79 if (i>0) {
80 TRACE(":");
81 }
82 TRACE(std::hex << (unsigned int)(buf[i]));
83
84 if (!this->expectAnything) {
85 if (this->expectBuffer->available()) {
86 uint8_t expected = this->expectBuffer->next();
87 if (expected != buf[i]) {
88 this->_error = true;
89 TRACE("!=" << (unsigned int)expected);
90 }
91 } else {
92 this->_error = true;
93 }
94 }
95 }
96 TRACE("\n"<<std::dec);
97 return size;
98}
99int ShimClient::available() {
100 return this->responseBuffer->available();
101}
102int ShimClient::read() { return this->responseBuffer->next(); }
103int ShimClient::read(uint8_t *buf, size_t size) {
104 uint16_t i = 0;
105 for (;i<size;i++) {
106 buf[i] = this->read();
107 }
108 return size;
109}
110int ShimClient::peek() { return 0; }
111void ShimClient::flush() {}
112void ShimClient::stop() {
113 this->setConnected(false);
114}
115uint8_t ShimClient::connected() { return this->_connected; }
116ShimClient::operator bool() { return true; }
117
118
119ShimClient* ShimClient::respond(uint8_t *buf, size_t size) {
120 this->responseBuffer->add(buf,size);
121 return this;
122}
123
124ShimClient* ShimClient::expect(uint8_t *buf, size_t size) {
125 this->expectAnything = false;
126 this->expectBuffer->add(buf,size);
127 return this;
128}
129
130void ShimClient::setConnected(bool b) {
131 this->_connected = b;
132}
133void ShimClient::setAllowConnect(bool b) {
134 this->_allowConnect = b;
135}
136
137bool ShimClient::error() {
138 return this->_error;
139}
140
141uint16_t ShimClient::received() {
142 return this->_received;
143}
144
145void ShimClient::expectConnect(IPAddress ip, uint16_t port) {
146 this->_expectedIP = ip;
147 this->_expectedPort = port;
148}
149
150void ShimClient::expectConnect(const char *host, uint16_t port) {
151 this->_expectedHost = host;
152 this->_expectedPort = port;
153}
Note: See TracBrowser for help on using the repository browser.