[175] | 1 | /**
|
---|
| 2 | * \file
|
---|
| 3 | * ESP8266 bridge arduino library
|
---|
| 4 | * \author
|
---|
| 5 | * Tuan PM <tuanpm@live.com>
|
---|
| 6 | */
|
---|
| 7 | #include "espduino.h"
|
---|
| 8 |
|
---|
| 9 | RESPONSE::RESPONSE(void * response)
|
---|
| 10 | {
|
---|
| 11 | cmd = (PACKET_CMD*)response;
|
---|
| 12 | arg_ptr = (uint8_t*)&cmd->args;
|
---|
| 13 | arg_num = 0;
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | uint16_t RESPONSE::getArgc()
|
---|
| 17 | {
|
---|
| 18 | return cmd->argc;
|
---|
| 19 | }
|
---|
| 20 | uint16_t RESPONSE::argLen()
|
---|
| 21 | {
|
---|
| 22 | return *(uint16_t*)arg_ptr;
|
---|
| 23 | }
|
---|
| 24 | int32_t RESPONSE::popArgs(uint8_t *data, uint16_t maxLen)
|
---|
| 25 | {
|
---|
| 26 | uint16_t length, len, incLen = 0;
|
---|
| 27 |
|
---|
| 28 | if(arg_num >= cmd->argc)
|
---|
| 29 | return -1;
|
---|
| 30 |
|
---|
| 31 | length = *(uint16_t*)arg_ptr;
|
---|
| 32 | len = length;
|
---|
| 33 | arg_ptr += 2;
|
---|
| 34 |
|
---|
| 35 | while(length --){
|
---|
| 36 | *data ++ = *arg_ptr ++;
|
---|
| 37 | incLen ++;
|
---|
| 38 | if(incLen > maxLen){
|
---|
| 39 | arg_num ++;
|
---|
| 40 | return maxLen;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | }
|
---|
| 44 | arg_num ++;
|
---|
| 45 | return len;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | String RESPONSE::popString()
|
---|
| 49 | {
|
---|
| 50 | String ret;
|
---|
| 51 | uint16_t len = *(uint16_t*)arg_ptr;
|
---|
| 52 | arg_ptr += 2;
|
---|
| 53 | while(len --)
|
---|
| 54 | ret += (char)*arg_ptr ++;
|
---|
| 55 | arg_num ++;
|
---|
| 56 | return ret;
|
---|
| 57 | }
|
---|
| 58 | void RESPONSE::popString(String* data)
|
---|
| 59 | {
|
---|
| 60 | uint16_t len = *(uint16_t*)arg_ptr;
|
---|
| 61 | arg_ptr += 2;
|
---|
| 62 | while(len --)
|
---|
| 63 | data->concat( (char)*arg_ptr ++);
|
---|
| 64 | arg_num ++;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | void ESP::protoCompletedCb(void)
|
---|
| 68 | {
|
---|
| 69 | PACKET_CMD *cmd = (PACKET_CMD*)_proto.buf;
|
---|
| 70 | uint16_t crc = 0, argc, len, resp_crc;
|
---|
| 71 | uint8_t *data_ptr;
|
---|
| 72 | argc = cmd->argc;
|
---|
| 73 | data_ptr = (uint8_t*)&cmd->args ;
|
---|
| 74 | crc = crc16_data((uint8_t*)&cmd->cmd, 12, crc);
|
---|
| 75 | while(argc--){
|
---|
| 76 | len = *((uint16_t*)data_ptr);
|
---|
| 77 | crc = crc16_data(data_ptr, 2, crc);
|
---|
| 78 | data_ptr += 2;
|
---|
| 79 | while(len --){
|
---|
| 80 | crc = crc16_data(data_ptr, 1, crc);
|
---|
| 81 | data_ptr ++;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | resp_crc = *(uint16_t*)data_ptr;
|
---|
| 85 | if(crc != resp_crc) {
|
---|
| 86 | INFO("ARDUINO: Invalid CRC");
|
---|
| 87 | return;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | FP<void, void*> *fp;
|
---|
| 91 | if(cmd->callback != 0){
|
---|
| 92 | fp = (FP<void, void*>*)cmd->callback;
|
---|
| 93 |
|
---|
| 94 | return_cmd = cmd->cmd;
|
---|
| 95 | return_value = cmd->_return;
|
---|
| 96 |
|
---|
| 97 | if(fp->attached())
|
---|
| 98 | (*fp)((void*)cmd);
|
---|
| 99 | } else {
|
---|
| 100 | if(cmd->argc == 0) {
|
---|
| 101 | is_return = true;
|
---|
| 102 | return_cmd = cmd->cmd;
|
---|
| 103 | return_value = cmd->_return;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | void ESP::wifiConnect(const char* ssid, const char* password)
|
---|
| 110 | {
|
---|
| 111 | uint16_t crc;
|
---|
| 112 | crc = request(CMD_WIFI_CONNECT, (uint32_t)&wifiCb, 0, 2);
|
---|
| 113 | crc = request(crc,(uint8_t*)ssid, strlen(ssid));
|
---|
| 114 | crc = request(crc,(uint8_t*)password, strlen(password));
|
---|
| 115 | request(crc);
|
---|
| 116 | }
|
---|
| 117 | void ESP::write(uint8_t data)
|
---|
| 118 | {
|
---|
| 119 | switch(data){
|
---|
| 120 | case SLIP_START:
|
---|
| 121 | case SLIP_END:
|
---|
| 122 | case SLIP_REPL:
|
---|
| 123 | _serial->write(SLIP_REPL);
|
---|
| 124 | _serial->write(SLIP_ESC(data));
|
---|
| 125 | break;
|
---|
| 126 | default:
|
---|
| 127 | _serial->write(data);
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 | void ESP::write(uint8_t* data, uint16_t len)
|
---|
| 131 | {
|
---|
| 132 | while(len --)
|
---|
| 133 | write(*data ++);
|
---|
| 134 | }
|
---|
| 135 | uint16_t ESP::request(uint16_t cmd, uint32_t callback, uint32_t _return, uint16_t argc)
|
---|
| 136 | {
|
---|
| 137 | uint16_t crc = 0;
|
---|
| 138 | _serial->write(0x7E);
|
---|
| 139 | write((uint8_t*)&cmd, 2);
|
---|
| 140 | crc = crc16_data((uint8_t*)&cmd, 2, crc);
|
---|
| 141 |
|
---|
| 142 | write((uint8_t*)&callback, 4);
|
---|
| 143 | crc = crc16_data((uint8_t*)&callback, 4, crc);
|
---|
| 144 |
|
---|
| 145 | write((uint8_t*)&_return, 4);
|
---|
| 146 | crc = crc16_data((uint8_t*)&_return, 4, crc);
|
---|
| 147 |
|
---|
| 148 | write((uint8_t*)&argc, 2);
|
---|
| 149 | crc = crc16_data((uint8_t*)&argc, 2, crc);
|
---|
| 150 | return crc;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | uint16_t ESP::request(uint16_t crc_in, uint8_t* data, uint16_t len)
|
---|
| 154 | {
|
---|
| 155 | uint8_t temp = 0;
|
---|
| 156 | uint16_t pad_len = len;
|
---|
| 157 | while(pad_len % 4 != 0)
|
---|
| 158 | pad_len++;
|
---|
| 159 | write((uint8_t*)&pad_len, 2);
|
---|
| 160 | crc_in = crc16_data((uint8_t*)&pad_len, 2, crc_in);
|
---|
| 161 | while(len --){
|
---|
| 162 | write(*data);
|
---|
| 163 | crc_in = crc16_data((uint8_t*)data, 1, crc_in);
|
---|
| 164 | data ++;
|
---|
| 165 | if(pad_len > 0) pad_len --;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | while(pad_len --){
|
---|
| 169 | write(temp);
|
---|
| 170 | crc_in = crc16_data((uint8_t*)&temp, 1, crc_in);
|
---|
| 171 | }
|
---|
| 172 | return crc_in;
|
---|
| 173 | }
|
---|
| 174 | uint16_t ESP::request(uint16_t crc)
|
---|
| 175 | {
|
---|
| 176 | write((uint8_t*)&crc, 2);
|
---|
| 177 | _serial->write(0x7F);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | void ESP::init()
|
---|
| 181 | {
|
---|
| 182 | _proto.buf = _protoBuf;
|
---|
| 183 | _proto.bufSize = sizeof(_protoBuf);
|
---|
| 184 | _proto.dataLen = 0;
|
---|
| 185 | _proto.isEsc = 0;
|
---|
| 186 | pinMode(_chip_pd, OUTPUT);
|
---|
| 187 |
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[224] | 190 | ESP::ESP(Stream *serial):
|
---|
| 191 | _serial(serial)
|
---|
[175] | 192 | {
|
---|
| 193 | _debugEn = false;
|
---|
| 194 | init();
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[224] | 197 |
|
---|
[175] | 198 | void ESP::enable()
|
---|
| 199 | {
|
---|
| 200 | digitalWrite(_chip_pd, HIGH);
|
---|
| 201 | }
|
---|
| 202 | void ESP::disable()
|
---|
| 203 | {
|
---|
| 204 | digitalWrite(_chip_pd, LOW);
|
---|
| 205 | }
|
---|
| 206 | void ESP::INFO(String info)
|
---|
| 207 | {
|
---|
| 208 | if(_debugEn)
|
---|
| 209 | _debug->println(info);
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | void ESP::reset()
|
---|
| 213 | {
|
---|
| 214 | uint16_t crc = request(CMD_RESET, 0, 0, 0);
|
---|
| 215 | request(crc);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | boolean ESP::ready()
|
---|
| 219 | {
|
---|
| 220 | uint32_t wait;
|
---|
| 221 |
|
---|
| 222 | for(uint8_t wait_time=5; wait_time>0; wait_time--){
|
---|
| 223 | is_return = false;
|
---|
| 224 | return_value = 0;
|
---|
| 225 | uint16_t crc = request(CMD_IS_READY, 0, 1, 0);
|
---|
| 226 | request(crc);
|
---|
| 227 | wait = millis();
|
---|
| 228 | while(is_return == false && (millis() - wait < 1000)) {
|
---|
| 229 | process();
|
---|
| 230 | }
|
---|
| 231 | if(is_return && return_value)
|
---|
| 232 | return true;
|
---|
| 233 | }
|
---|
| 234 | return false;
|
---|
| 235 |
|
---|
| 236 | }
|
---|
| 237 | boolean ESP::waitReturn(uint32_t timeout)
|
---|
| 238 | {
|
---|
| 239 | is_return = false;
|
---|
| 240 | return_value = 0;
|
---|
| 241 | return_cmd = 0;
|
---|
| 242 | uint32_t wait = millis();
|
---|
| 243 | while(is_return == false && (millis() - wait < timeout)) {
|
---|
| 244 | process();
|
---|
| 245 | }
|
---|
| 246 | return is_return;
|
---|
| 247 | }
|
---|
| 248 | boolean ESP::waitReturn()
|
---|
| 249 | {
|
---|
| 250 | return waitReturn(ESP_TIMEOUT);
|
---|
| 251 | }
|
---|
| 252 | void ESP::process()
|
---|
| 253 | {
|
---|
| 254 | char value;
|
---|
| 255 | while(_serial->available()) {
|
---|
| 256 | value = _serial->read();
|
---|
| 257 | switch(value){
|
---|
| 258 | case 0x7D:
|
---|
| 259 | _proto.isEsc = 1;
|
---|
| 260 | break;
|
---|
| 261 |
|
---|
| 262 | case 0x7E:
|
---|
| 263 | _proto.dataLen = 0;
|
---|
| 264 | _proto.isEsc = 0;
|
---|
| 265 | _proto.isBegin = 1;
|
---|
| 266 | break;
|
---|
| 267 |
|
---|
| 268 | case 0x7F:
|
---|
| 269 | protoCompletedCb();
|
---|
| 270 | _proto.isBegin = 0;
|
---|
| 271 | break;
|
---|
| 272 |
|
---|
| 273 | default:
|
---|
| 274 | if(_proto.isBegin == 0) {
|
---|
| 275 | if(_debugEn) {
|
---|
| 276 | _debug->write(value);
|
---|
| 277 | }
|
---|
| 278 | break;
|
---|
| 279 | }
|
---|
| 280 | if(_proto.isEsc){
|
---|
| 281 | value ^= 0x20;
|
---|
| 282 | _proto.isEsc = 0;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | if(_proto.dataLen < _proto.bufSize)
|
---|
| 286 | _proto.buf[_proto.dataLen++] = value;
|
---|
| 287 |
|
---|
| 288 | break;
|
---|
| 289 | }
|
---|
| 290 | }
|
---|
| 291 | }
|
---|