Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Unifying codestyle
  • Loading branch information
andreagilardoni committed Jul 17, 2024
commit c78fd2b81dc597bc1552414970438fcb5bab75c5
101 changes: 41 additions & 60 deletions libraries/WiFiS3/src/Modem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,110 +9,91 @@ using namespace std;
/* -------------------------------------------------------------------------- */
ModemClass::ModemClass(int tx, int rx) : beginned(false), delete_serial(false), _timeout(MODEM_TIMEOUT), trim_results(true), read_by_size(false) {
/* -------------------------------------------------------------------------- */
_serial = new UART(tx,rx);
_serial = new UART(tx,rx);
}

/* -------------------------------------------------------------------------- */
ModemClass::ModemClass(UART * serial) : beginned(false) , delete_serial(true) , _serial(serial), _timeout(MODEM_TIMEOUT), trim_results(true), read_by_size(false) {
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
}

/* -------------------------------------------------------------------------- */
ModemClass::~ModemClass() {
/* -------------------------------------------------------------------------- */
if(_serial != nullptr && !delete_serial){
/* -------------------------------------------------------------------------- */
if(_serial != nullptr && !delete_serial){
delete _serial;
_serial = nullptr;
}
}
}

/* -------------------------------------------------------------------------- */
void ModemClass::begin(int badurate){
/* -------------------------------------------------------------------------- */
if(_serial != nullptr && !beginned) {
/* -------------------------------------------------------------------------- */
if(_serial != nullptr && !beginned) {
_serial->begin(badurate);
beginned = true;
string res = "";
_serial->flush();
modem.write(string(PROMPT(_SOFTRESETWIFI)),res, "%s" , CMD(_SOFTRESETWIFI));
}
}
}

/* -------------------------------------------------------------------------- */
void ModemClass::end(){
/* -------------------------------------------------------------------------- */
_serial->end();
/* -------------------------------------------------------------------------- */
_serial->end();
}

/* -------------------------------------------------------------------------- */
bool ModemClass::passthrough(const uint8_t *data, size_t size) {
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
_serial->write(data,size);
bool res = false;
bool found = false;
string data_res = "";

unsigned long start_time = millis();
while(millis() - start_time < _timeout && !found){
while(_serial->available()){
char c = _serial->read();
data_res += c;

if(string::npos != data_res.rfind(RESULT_OK)){
found = true;
res = true;
break;
}
else if (string::npos != data_res.rfind(RESULT_ERROR)) {
found = true;
res = false;
break;
}
}
}

if(_serial_debug && _debug_level >= 2) {
_serial_debug->print(" ANSWER (passthrough): ");
_serial_debug->println(data_res.c_str());
if(res) {
_serial_debug->println(" Result: OK");
}
else {
_serial_debug->println(" Result: FAILED");
}
}


std::string tmp, data_res; // FIXME
bool res = buf_read(tmp, data_res);

// if(_serial_debug && _debug_level >= 2) {
// _serial_debug->print(" ANSWER (passthrough): ");
// _serial_debug->println(data_res.c_str());
// if(res) {
// _serial_debug->println(" Result: OK");
// }
// else {
// _serial_debug->println(" Result: FAILED");
// }
// }

return res;
}

/* -------------------------------------------------------------------------- */
void ModemClass::write_nowait(const string &cmd, string &str, const char * fmt, ...) {
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
va_list va;
va_start (va, fmt);
vsnprintf((char *)tx_buff, MAX_BUFF_SIZE, fmt, va);
va_end (va);
if(_serial_debug && _debug_level >= 2) {

if(_serial_debug && _debug_level >= 2) {
_serial_debug->print("REQUEST (passthrough): ");
_serial_debug->write(tx_buff,strlen((char *)tx_buff));
_serial_debug->println();
}

_serial->write(tx_buff,strlen((char *)tx_buff));
return;
}


/* -------------------------------------------------------------------------- */
bool ModemClass::write(const string &prompt, string &data_res, const char * fmt, ...){
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
data_res.clear();
va_list va;
va_start (va, fmt);
vsnprintf((char *)tx_buff, MAX_BUFF_SIZE, fmt, va);
va_end (va);

if(_serial_debug) {
_serial_debug->println();
_serial_debug->print("REQUEST: ");
Expand All @@ -121,7 +102,7 @@ bool ModemClass::write(const string &prompt, string &data_res, const char * fmt,
}

_serial->write(tx_buff,strlen((char *)tx_buff));
return buf_read(prompt,data_res);;
return buf_read(prompt, data_res);;
}


Expand All @@ -134,7 +115,7 @@ typedef enum {

/* -------------------------------------------------------------------------- */
bool ModemClass::read_by_size_finished(string &rx) {
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
bool rv = false;
static bool first_call = true;
static ReadBySizeSt_t st = IDLE;
Expand Down Expand Up @@ -194,7 +175,7 @@ bool ModemClass::read_by_size_finished(string &rx) {

/* -------------------------------------------------------------------------- */
bool ModemClass::buf_read(const string &prompt, string &data_res) {
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
bool res = false;
bool found = false;

Expand Down Expand Up @@ -262,6 +243,7 @@ bool ModemClass::buf_read(const string &prompt, string &data_res) {
}
}
}

if(trim_results) {
trim(data_res);
}
Expand All @@ -273,23 +255,22 @@ bool ModemClass::buf_read(const string &prompt, string &data_res) {
_serial_debug->println();
}

if(_serial_debug) {
if(_serial_debug) {
_serial_debug->print(" ANSWER: ");
_serial_debug->println(data_res.c_str());
if(res) {
_serial_debug->println(" Result: OK");
}
else {
_serial_debug->println(" Result: FAILED");
}
}

}
}

return res;
}

#ifdef ARDUINO_UNOWIFIR4
ModemClass modem = ModemClass(&Serial2);
ModemClass modem = ModemClass(&Serial2);
#else
ModemClass modem = ModemClass(D24,D25);
ModemClass modem = ModemClass(D24,D25);
#endif
6 changes: 3 additions & 3 deletions libraries/WiFiS3/src/Modem.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ class ModemClass {

void read_using_size() {
read_by_size = true;
}
}
bool beginned;

/* calling this function with no argument will enable debug message to be printed
on Serial
use first parameter UART *u to redirect debug output to a different serial
use first parameter UART *u to redirect debug output to a different serial

level from 0 defaul to 2 (maximum) */

void debug(Stream &u, uint8_t level = 0) {
_serial_debug = &u;

if(level > 2) {
level = 2;
}
Expand Down
Loading