Skip to content

Commit e8cec8a

Browse files
committed
[drivewire][network] protocol refinements.
1 parent 3622036 commit e8cec8a

File tree

1 file changed

+57
-21
lines changed

1 file changed

+57
-21
lines changed

lib/device/drivewire/network.cpp

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,21 @@ void drivewireNetwork::open()
129129
{
130130
Debug_printf("drivewireNetwork::sio_open(%02x,%02x)\n",cmdFrame.aux1,cmdFrame.aux2);
131131

132-
deviceSpec.clear();
133-
134-
while (fnUartBUS.available())
135-
{
136-
deviceSpec += fnUartBUS.read();
137-
}
132+
char tmp[256];
133+
memset(tmp,0,sizeof(tmp));
134+
135+
size_t bytes_read = fnUartBUS.readBytes(tmp,256);
138136

137+
Debug_printf("tmp = %s\n",tmp);
138+
139+
if (bytes_read != 256)
140+
{
141+
Debug_printf("Short read of %lu bytes. Exiting.",bytes_read);
142+
return;
143+
}
139144

145+
deviceSpec = std::string(tmp,256);
146+
140147
channelMode = PROTOCOL;
141148

142149
// Delete timer if already extant.
@@ -555,9 +562,17 @@ void drivewireNetwork::get_prefix()
555562
void drivewireNetwork::set_prefix()
556563
{
557564
std::string prefixSpec_str;
565+
char tmp[256];
566+
memset(tmp,0,sizeof(tmp));
567+
size_t read_bytes = fnUartBUS.readBytes(tmp,256);
558568

559-
while (fnUartBUS.available())
560-
prefixSpec_str += fnUartBUS.read();
569+
if (read_bytes != 256)
570+
{
571+
Debug_printf("Short read by %lu bytes. Exiting.",read_bytes);
572+
return;
573+
}
574+
575+
prefixSpec_str = std::string(tmp,256);
561576

562577
prefixSpec_str = prefixSpec_str.substr(prefixSpec_str.find_first_of(":") + 1);
563578
Debug_printf("drivewireNetwork::set_prefix(%s)\n", prefixSpec_str.c_str());
@@ -651,9 +666,18 @@ void drivewireNetwork::set_channel_mode()
651666
*/
652667
void drivewireNetwork::set_login()
653668
{
654-
login.clear();
655-
while (fnUartBUS.available())
656-
login += fnUartBUS.read();
669+
char tmp[256];
670+
memset(tmp,0,sizeof(tmp));
671+
672+
size_t bytes_read = fnUartBUS.readBytes(tmp,256);
673+
674+
if (bytes_read != 256)
675+
{
676+
Debug_printf("Short read of %lu bytes. Exiting.\n",bytes_read);
677+
return;
678+
}
679+
680+
login = std::string(tmp,256);
657681

658682
Debug_printf("drivewireNetwork::set_login(%s)\n",login.c_str());
659683
}
@@ -663,9 +687,16 @@ void drivewireNetwork::set_login()
663687
*/
664688
void drivewireNetwork::set_password()
665689
{
666-
password.clear();
667-
while (fnUartBUS.available())
668-
password += fnUartBUS.read();
690+
char tmp[256];
691+
memset(tmp,0,sizeof(tmp));
692+
693+
size_t bytes_read = fnUartBUS.readBytes(tmp,256);
694+
695+
if (bytes_read != 256)
696+
{
697+
Debug_printf("Short read of %lu bytes. Exiting.\n",bytes_read);
698+
return;
699+
}
669700

670701
Debug_printf("drivewireNetwork::set_password(%s)\n",password.c_str());
671702
}
@@ -858,8 +889,8 @@ void drivewireNetwork::special_80()
858889
memset(spData, 0, SPECIAL_BUFFER_SIZE);
859890

860891
// Get special (devicespec) from computer
861-
while (fnUartBUS.available())
862-
spData[i++]=fnUartBUS.read();
892+
893+
fnUartBUS.readBytes(spData,256);
863894

864895
Debug_printf("drivewireNetwork::special_80() - %s\n", spData);
865896

@@ -1185,22 +1216,27 @@ void drivewireNetwork::parse_json()
11851216
void drivewireNetwork::json_query()
11861217
{
11871218
std::string in_string;
1188-
uint16_t len = get_daux();
1219+
char tmpq[256];
1220+
memset(tmpq,0,sizeof(tmpq));
11891221

1190-
while (len)
1222+
size_t bytes_read = fnUartBUS.readBytes(tmpq,256);
1223+
1224+
if (bytes_read != 256)
11911225
{
1192-
in_string += fnUartBUS.read();
1193-
len--;
1226+
Debug_printf("Short read of %lu bytes. Exiting\n",bytes_read);
1227+
return;
11941228
}
11951229

1230+
in_string = std::string(tmpq,256);
1231+
11961232
// strip away line endings from input spec.
11971233
for (int i = 0; i < in_string.size(); i++)
11981234
{
11991235
if (in_string[i] == 0x0A || in_string[i] == 0x0D || in_string[i] == 0x9b)
12001236
in_string[i] = 0x00;
12011237
}
12021238

1203-
json->setReadQuery(in_string, cmdFrame.aux2);
1239+
json->setReadQuery(in_string, 256);
12041240
json_bytes_remaining = json->json_bytes_remaining;
12051241

12061242
std::vector<uint8_t> tmp(json_bytes_remaining);

0 commit comments

Comments
 (0)