Process message receive

This commit is contained in:
Juhani Krekelä 2019-07-14 20:36:12 +03:00
parent c19d05c64e
commit 59cce635c7
2 changed files with 22 additions and 20 deletions

View File

@ -777,26 +777,17 @@ void handle_message(const unsigned char source_mac[6], const unsigned char *data
return;
}
char mac[18];
format_mac(source_mac, mac);
// Type of event: Received a message
writeallx(1, "m", 1);
if (printf("%s (%" PRIu16 "): ", mac, msgid) == -1) {
err(1, "printf");
}
// MAC
writeallx(1, source_mac, 6);
for (size_t i = 0; i < message_length; i++) {
if (putchar(message[i]) == EOF) {
err(1, "putchar");
}
}
// Message length
writeallx_u16(1, message_length);
if (putchar('\n') == EOF) {
err(1, "putchar");
}
if (fflush(stdout) == EOF) {
err(1, "fflush");
}
// Message
writeallx(1, message, message_length);
send_ack(source_mac, msgid);
}

View File

@ -19,6 +19,10 @@ def readall(f, length):
read.extend(data)
return bytes(read)
def readall_u16(f):
u16_bytes = readall(f, 2)
return (u16_bytes[1] << 8) | u16_bytes[0]
def parse_mac(text):
parts = text.split(':')
if len(parts) != 6:
@ -88,9 +92,16 @@ def eventloop(proc):
elif event_type == b'i':
# Msgid for message
msgid_bytes = readall(proc.stdout, 2)
msgid = (msgid_bytes[1] << 8) | msgid_bytes[0]
print('Msgid: %i' % msgid)
msgid = readall_u16(proc.stdout);
print('(msgid: %i)' % msgid) #debg
elif event_type == b'm':
# Message received
source_mac = readall(proc.stdout, 6)
message_length = readall_u16(proc.stdout)
message = readall(proc.stdout, message_length)
print('<%s> %s' % (format_mac(source_mac), message.decode('utf-8')))
else:
# Not sth we handle yet