Gbox 4.20
Grow box automation and monitoring - <a href='https://sites.google.com/site/growboxguy/'>https://sites.google.com/site/growboxguy/</a>
 
Loading...
Searching...
No Matches
ELClientSocket.cpp
Go to the documentation of this file.
1
10#include "ELClientSocket.h"
11
29
39void ELClientSocket::socketCallback(void *res)
40{
41 if (!res) return;
42
44
45 #ifdef DEBUG_EN
46 int argNum = resp->argc();
47 Serial.println("Number of arguments: "+String(argNum));
48 uint16_t _cmd = resp->cmd();
49 Serial.println("Command: "+String(_cmd));
50 uint16_t _value = resp->value();
51 Serial.println("Value: "+String(_value));
52 #endif
53
54 resp->popArg(&_resp_type, 1);
55 resp->popArg(&_client_num, 1);
56 resp->popArg(&_len, 2);
57 #ifdef DEBUG_EN
58 Serial.print("Type: ");
59 Serial.print(_resp_type);
60 Serial.print(" client: ");
61 Serial.print(_client_num);
62 Serial.print(" size: "+String(_len));
63 #endif
64 if (_resp_type == 1)
65 {
66 #ifdef DEBUG_EN
67 int argLen = resp->argLen();
68 Serial.print(" data length: "+String(argLen));
69 #endif
70 resp->popArgPtr((void**)&_data);
71 #ifdef DEBUG_EN
72 _data[_len] = '\0';
73 Serial.print(" data: "+String(_data));
74 #endif
75 }
76 #ifdef DEBUG_EN
77 Serial.println("");
78 #endif
79 _status = 1;
80 if (_hasUserCb)
81 {
82 _userCb(_resp_type, _client_num, _len, _data);
83 }
84}
85
121int ELClientSocket::begin(const char* host, uint16_t port, uint8_t sock_mode, void (*userCb)(uint8_t resp_type, uint8_t client_num, uint16_t len, char *data))
122{
123 if (userCb != 0)
124 {
125 _userCb = userCb;
126 _hasUserCb = true;
127 }
128
129 socketCb.attach(this, &ELClientSocket::socketCallback);
130
131 _elc->Request(CMD_SOCKET_SETUP, (uint32_t)&socketCb, 3);
132 _elc->Request(host, strlen(host));
133 _elc->Request(&port, 2);
134 _elc->Request(&sock_mode, 1);
135 _elc->Request();
136
137 ELClientPacket *pkt = _elc->WaitReturn();
138
139 if (pkt && (int32_t)pkt->value >= 0)
140 {
141 remote_instance = pkt->value;
142 // return 0;
143 }
144 return (int)pkt->value;
145}
146
160void ELClientSocket::send(const char* data, int len)
161{
162 _status = 0;
163 if (remote_instance < 0) return;
165 _elc->Request(data, strlen(data));
166 if (data != NULL && len > 0)
167 {
168 _elc->Request(data, len);
169 }
170
171 _elc->Request();
172}
173
184void ELClientSocket::send(const char* data)
185{
186 send(data, strlen(data));
187}
188
236uint16_t ELClientSocket::getResponse(uint8_t *resp_type, uint8_t *client_num, char* data, uint16_t maxLen)
237{
238 if (_status == 0) return 0;
239 memcpy(data, _data, _len>maxLen?maxLen:_len);
240 *resp_type = _resp_type;
241 *client_num = _client_num;
242 _status = 0;
243 return _len;
244}
245
306uint16_t ELClientSocket::waitResponse(uint8_t *resp_type, uint8_t *client_num, char* data, uint16_t maxLen, uint32_t timeout)
307{
308 uint32_t wait = millis();
309 while (_status == 0) {
310 if ( millis() - wait < timeout)
311 {
312 _elc->Process();
313 } else {
314 return -3;
315 }
316 }
317 return getResponse(resp_type, client_num, data, maxLen);
318}
Definitions for ELClientSocket.
@ CMD_SOCKET_SEND
Definition ELClient.h:42
@ CMD_SOCKET_SETUP
Definition ELClient.h:41
int16_t popArg(void *data, uint16_t maxLen)
Extract an argument from the response packet.
int16_t popArgPtr(void **data)
Extract pointer to an argument from the response packet.
uint16_t getResponse(uint8_t *resp_type, uint8_t *client_num, char *data, uint16_t maxLen)
Retrieve response.
void send(const char *data)
Send null-terminated data to the remote server.
int32_t remote_instance
uint16_t waitResponse(uint8_t *resp_type, uint8_t *client_num, char *data, uint16_t maxLen, uint32_t timeout=DEFAULT_SOCKET_TIMEOUT)
Wait for the response.
int begin(const char *host, uint16_t port, uint8_t sock_mode, void(*userCb)(uint8_t resp_type, uint8_t client_num, uint16_t len, char *data)=0)
Initialize communication to a remote server.
ELClientSocket(ELClient *e)
Class to send/receive data.
ELClientPacket * Process(void)
Handle serial input.
Definition ELClient.cpp:115
void Request(uint16_t cmd, uint32_t value, uint16_t argc)
Start a request.
Definition ELClient.cpp:211
ELClientPacket * WaitReturn(uint32_t timeout=ESP_TIMEOUT)
Wait for a response from ESP for a given timeout.
Definition ELClient.cpp:444
void attach(T *item, retT(T::*method)(argT))
Definition FP.h:147
ESP esp & Serial
Definition demo.ino:9
uint32_t value
uint32_t wait