2 const char*
const BayTCPInterface::_urlencodedChars=
"$&+,/:;=?@ <>#%{}|~[]`";
4 const uint8_t* BayTCPInterface::parseMAC(
const char* str){
26 for(uint8_t i=0;i<6;i++){
27 _mac_ip[i]=(hex2int(str[offset])<<4)+hex2int(str[offset+1]);
36 uint8_t BayTCPInterface::hex2int(
const char c){
37 if((c>=
'0') & (c<=
'9'))
return c-
'0';
38 if((c>=
'A') & (c<=
'F'))
return c-
'A'+10;
39 if((c>=
'a') & (c<=
'f'))
return c-
'a'+10;
43 const uint8_t* BayTCPInterface::parseIP(
const char* str){
45 for(uint8_t i=0;i<4;i++){
46 _mac_ip[i]=atoi(str+offset);
47 while(str[offset] && str[offset]!=
'.') offset++;
53 void BayTCPInterface::printPGM(
const char *str){
58 #if BayTCP_DEBUG_INPUT 66 void BayTCPInterface::printlnPGM(
const char *str){
69 #if BayTCP_DEBUG_INPUT 74 void BayTCPInterface::skipChars(
void){
77 #if BayTCP_DEBUG_OUTPUT 78 Serial.print((
char) read());
85 }
while(i_available());
90 int BayTCPInterface::strlenURLencoded(
const char *str){
94 if(strchr(_urlencodedChars,*str)){
104 void BayTCPInterface::printURLencoded(
const char *str){
110 if(strchr(_urlencodedChars,*str)){
120 void BayTCPInterface::printPostHeader(uint16_t size){
123 printlnP(
" HTTP/1.1");
124 printP(
"Authorization: Basic ");
125 strcpy(_pgm_buffer,_user);
126 strcat(_pgm_buffer,
":");
127 strcat(_pgm_buffer,_password);
128 base64_encode(_base64buffer,(
char*) _pgm_buffer,strlen(_pgm_buffer));
129 _base64buffer[base64_enc_len(strlen(_pgm_buffer))]=0;
130 println(_base64buffer);
131 #if BayTCP_DEBUG_INPUT 132 Serial.println(_base64buffer);
137 #if BayTCP_DEBUG_INPUT 138 Serial.println(_server);
141 printlnP(
"User-Agent: BayTCP");
142 printlnP(
"Content-Type: application/x-www-form-urlencoded");
143 printlnP(
"Connection: close");
144 printP(
"Content-Length: ");
146 #if BayTCP_DEBUG_INPUT 147 Serial.println(size);
163 unsigned long readpos=_buffer->readPos();
168 if(_buffer->
available()>((
unsigned long) maxsize)){
174 if(size<150) size=150;
175 size+=7+strlenURLencoded(_sender)+1+9+strlenURLencoded(_password);
176 if(size>maxsize) size=maxsize;
178 printPostHeader(size);
183 printURLencoded(_sender);
184 printP(
"&password=");
185 printURLencoded(_password);
186 uint16_t postsize=7+strlenURLencoded(_sender)+1+9+strlenURLencoded(_password);
187 uint16_t mtusize=postsize;
192 if(_mtu && mtusize>_mtu){
198 framesize=1+15+strlenURLencoded(_base64buffer);
202 printP(
"&bayeosframes[]=");
203 printURLencoded(_base64buffer);
212 while(postsize<size){
213 if(_mtu && mtusize>_mtu){
224 finishTransmissionMode();
225 res=wait_for(
"HTTP/1.1 200",30000);
244 #if BayTCP_DEBUG_OUTPUT 245 Serial.println(
"sendPayload");
257 uint8_t size=strlenURLencoded(_base64buffer);
258 size+=7+strlenURLencoded(_sender)+1+9+strlenURLencoded(_password)+1+15;
260 printPostHeader(size);
267 printURLencoded(_sender);
268 printP(
"&password=");
269 printURLencoded(_password);
270 printP(
"&bayeosframes[]=");
271 printURLencoded(_base64buffer);
274 finishTransmissionMode();
275 res=wait_for(
"HTTP/1.1 200",30000);
277 if(res) _tx_error_count++;
278 else _tx_error_count=0;
286 uint8_t BayTCPInterface::addToConfigBuffer(uint8_t offset,
const char* str){
287 if(strlen(str)>BayTCP_CONFIG_SIZE -offset-1){
288 strncpy(_config_buffer+offset,str,BayTCP_CONFIG_SIZE -offset-1);
289 _config_buffer[BayTCP_CONFIG_SIZE -1]=0;
290 return BayTCP_CONFIG_SIZE -offset;
292 strcpy(_config_buffer+offset,str);
293 return strlen(str)+1;
299 _f.open(file, O_READ);
302 while((i<BayTCP_CONFIG_SIZE) && ((c=_f.read())!=-1)){
305 _config_buffer[i]=(char) c;
314 while (offset<BayTCP_CONFIG_SIZE) {
315 _config_buffer[offset]=pgm_read_byte(
string);
316 if (!_config_buffer[offset])
break;
317 if (_config_buffer[offset]==
'|') _config_buffer[offset]=0;
318 #if BayTCP_DEBUG_INPUT 319 Serial.write(_config_buffer[offset]);
328 if(EEPROM.read(eeoffset)==0xff){
332 for(uint8_t i=0;i<BayTCP_CONFIG_SIZE;i++){
333 _config_buffer[i]=EEPROM.read(eeoffset+i);
339 for(uint8_t i=0;i<BayTCP_CONFIG_SIZE;i++){
340 EEPROM.write(eeoffset+i,_config_buffer[i]);
345 return _config_buffer;
350 case BayTCP_CONFIG_SERVER:
353 case BayTCP_CONFIG_PORT:
356 case BayTCP_CONFIG_PATH:
359 case BayTCP_CONFIG_USER:
362 case BayTCP_CONFIG_PASSWORD:
365 case BayTCP_CONFIG_SENDER:
368 case BayTCP_CONFIG_APN:
371 case BayTCP_CONFIG_PROVPW:
374 case BayTCP_CONFIG_PROVUSER:
377 case BayTCP_CONFIG_PIN:
385 void BayTCPInterface::setConfigPointers(
void){
387 _server=_config_buffer;
389 for(uint8_t i=1;i<10;i++){
390 if(_config_buffer[offset])
391 offset+=strlen(_config_buffer+offset)+1;
394 *p=_config_buffer+offset;
399 memcpy(_base64buffer,_config_buffer,BayTCP_CONFIG_SIZE );
401 uint8_t offset_old=0;
403 for(uint8_t i=0;i<8;i++){
405 offset+=addToConfigBuffer(offset,str);
407 offset+=addToConfigBuffer(offset,_base64buffer+offset_old);
408 offset_old+=strlen(_base64buffer+offset_old)+1;
414 uint8_t BayTCPInterface::wait_for_available(uint16_t* timeout,
int bytes){
415 while(i_available()<bytes){
428 uint8_t BayTCPInterface::wait_forOK(uint16_t timeout){
429 return wait_forPGM(PSTR(
"OK"),timeout);
432 uint8_t BayTCPInterface::wait_forPGM(
const char* str,uint16_t timeout,uint8_t bytes,
char* buffer){
435 _pgm_buffer[length]=pgm_read_byte(str);
436 if (! _pgm_buffer[length])
break;
441 if(wait_for_available(&timeout,length))
446 if(wait_for_available(&timeout))
449 #if BayTCP_DEBUG_OUTPUT 454 if(c==_pgm_buffer[offset])
464 if(bytes && buffer!=NULL){
465 if(wait_for_available(&timeout,bytes))
469 buffer[offset]=read();
void seekReadPointer(unsigned long pos)
void readConfigFromStringPGM(const char *string)
virtual uint8_t connect(void)=0
void setConfig(const char *str, uint8_t index)
uint8_t getPacketLength(void) const
virtual void disconnect(void)=0
void readConfigFromEEPROM(int eeoffset=BayTCP_EEPROM_OFFSET)
void readConfigFromFile(const char *file)
uint8_t sendMultiFromBuffer(uint16_t maxsize=5000)
char ** getConfigPointer(uint8_t index)
unsigned long available(void)
uint8_t sendPayload(void)
const char * getConfig(void)
uint8_t readFromBuffer(void)
void writeConfigToEEPROM(int eeoffset=BayTCP_EEPROM_OFFSET)