Skip to content

Commit f205eb9

Browse files
committed
Start commons new send command
1 parent a838cc2 commit f205eb9

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import lightbug.devices as devices
2+
import lightbug.services as services
3+
import lightbug.messages as messages
4+
import log
5+
6+
// A simple application that sets up a Lightbug device and starts sending messages
7+
// Demonstrates both synchronous and asynchronous message sending.
8+
main:
9+
log.set-default (log.default.with-level log.INFO-LEVEL)
10+
11+
// RtkHandheld2 is a Lightbug device that uses I2C, and is pre-configured
12+
// Do not send an open or heartbeat messages
13+
device := devices.RtkHandheld2 --open=false
14+
15+
while true:
16+
/**
17+
Send an asynchronous message to open the device.
18+
The call to send is non-blocking, and the callback will be called when the response is received within the --timeout
19+
*/
20+
log.info "Calling send-new (async)..."
21+
latch := device.comms.send-new messages.Open.msg --async --callback=:: |result|
22+
if result and result.msg-ok:
23+
log.info "Device opened successfully (async): $result"
24+
else if result:
25+
log.error "Failed to open device (async): $result.msg-status"
26+
else:
27+
log.error "Failed to open device (async), no response received"
28+
log.info "Called send-new (async), waiting for response..."
29+
30+
// We don't have to wait for this, but we will do it anyway, so we don't conflict with the sync example below
31+
latch.get
32+
33+
/**
34+
Send a synchronous message to open the device.
35+
This will block until the response is received or the --timeout is reached.
36+
*/
37+
log.info "Calling send-new (sync)..."
38+
result := device.comms.send-new messages.Open.msg
39+
if result and result.msg-ok:
40+
log.info "Device opened successfully (sync): $result"
41+
else if result:
42+
log.error "Failed to open device (sync): $result.msg-status"
43+
else:
44+
log.error "Failed to open device (sync), no response received"
45+
log.info "Called send-new (sync)..."

src/modules/comms/comms.toit

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ class Comms:
240240
sendSwitching_ outbox_.receive --now=true
241241
yield // on each message sent
242242
243+
243244
// Send a message, and possibly do advanced things before after and during sending
244245
send msg/protocol.Message
245246
--now/bool = false // Should it be send now, of via the outbox
@@ -297,6 +298,46 @@ class Comms:
297298
return latch
298299
return null
299300

301+
send-new msg/protocol.Message
302+
--flush/bool = false
303+
--timeout/Duration = (Duration --s=60) -> protocol.Message?:
304+
// Ensure the message has a known message id
305+
if not (msg.header.data.has-data protocol.Header.TYPE-MESSAGE-ID):
306+
msg.header.data.add-data-uint32 protocol.Header.TYPE-MESSAGE-ID msgIdGenerator.next
307+
308+
logger_.with-level log.DEBUG-LEVEL:
309+
logger_.debug "SEND: $(msg)"
310+
311+
latch := monitor.Latch
312+
latchForMessage[msg.msgId] = latch
313+
waitTimeouts[msg.msgId] = Time.now + timeout
314+
315+
sendSwitching_ msg --now=flush
316+
317+
return latch.get
318+
319+
send-new msg/protocol.Message --async
320+
--callback/Lambda? = null
321+
--flush/bool = false
322+
--timeout/Duration = (Duration --s=60) -> monitor.Latch?:
323+
// Ensure the message has a known message id
324+
if not (msg.header.data.has-data protocol.Header.TYPE-MESSAGE-ID):
325+
msg.header.data.add-data-uint32 protocol.Header.TYPE-MESSAGE-ID msgIdGenerator.next
326+
327+
logger_.with-level log.DEBUG-LEVEL:
328+
logger_.debug "SEND: $(msg)"
329+
330+
latch := monitor.Latch
331+
latchForMessage[msg.msgId] = latch
332+
waitTimeouts[msg.msgId] = Time.now + timeout
333+
334+
sendSwitching_ msg --now=flush
335+
336+
if callback:
337+
task::
338+
callback.call latch.get
339+
return latch
340+
300341
// Send directly, or via the outbox
301342
sendSwitching_ msg/protocol.Message --now/bool?=false:
302343
// TODO don't call this on both the outbox and regular paths, as it is messy
@@ -358,7 +399,7 @@ class Comms:
358399

359400
// Remove the timeout key, complete the latch, and remove all callbacks
360401
waitTimeouts.remove key
361-
latchForMessage[key].set false // false currently means timeout?
402+
latchForMessage[key].set null // null, indicating there was no message received before the timeout
362403
latchForMessage.remove key
363404
lambdasForBadAck.remove key
364405
lambdasForGoodAck.remove key

src/protocol/message.toit

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class Message:
6262
if header.data.has-data Header.TYPE_MESSAGE_STATUS: return header.data.get-data-intn Header.TYPE_MESSAGE_STATUS
6363
return null
6464

65+
msg-ok -> bool:
66+
return header.data.has-data Header.TYPE_MESSAGE_STATUS and (header.data.get-data-intn Header.TYPE_MESSAGE_STATUS) <= Header.STATUS_OK
67+
6568
msg-status-id status/int -> bool:
6669
return header.data.has-data Header.TYPE_MESSAGE_STATUS and (header.data.get-data-intn Header.TYPE_MESSAGE_STATUS) == status
6770

0 commit comments

Comments
 (0)