Loading...
Searching...
No Matches
Data In and Out

The AppMessage pipeline to the phone and the byte readers behind it. More...

Files

file  src/c/pebble/io/appmessage/appmessage.c
 AppMessage transport: weather requests out, settings + readings in. Decodes the wire format and hands results to the app through handlers, so it never reaches into the UI itself.
file  src/c/pebble/io/appmessage/appmessage.h
 AppMessage transport. Decodes the inbox and notifies the app through per-channel handlers, and queues the outbound weather, stock, calendar, phone battery, and now playing requests through one outbox.
file  src/c/pebble/io/appmessage/appmessage_features.h
 Which of the phone's optional features a face has, worked out from the message keys it declares. The waf build defines a HAS_MESSAGE_KEY_* for every key in the face's messageKeys, and this turns those into one switch per feature, so every file asking the same question gets the same answer.
file  src/c/pebble/io/long_text.c
 The deciding behind the face's long text setting: when it can be set, whether it fits, what the reply carries, and whether an arrival changed it.
file  src/c/pebble/io/long_text.h
 One text setting too long for the settings blob, which rides its own message key and the face's own storage.
file  src/c/pebble/io/outbox_queue.h
 The outbox's work queue, its failed set, and the slot holding whatever is in flight.
file  src/c/pebble/io/tuple_read.c
 Reads a value out of an inbound message tuple.
file  src/c/pebble/io/tuple_read.h
 Reads a value out of an inbound message tuple.

Data Structures

struct  LongText
 The face's long text setting: its key, its cap, and the provider and receiver. More...
struct  OutboxJob
 One job waiting to go out, or already in flight. More...
struct  OutboxQueue
 Everything the outbox is holding: what is waiting, what nacked, and what is out. More...
struct  [struct].s_handlers

Macros

#define LONG_TEXT_CAP_MAX   1024
 The most bytes a long text can take, its end included. Its buffer sits on the stack for one send or compare.
#define OUTBOX_PHONE_SLOTS   0
 no phone battery request on this face
#define OUTBOX_MUSIC_SLOTS   0
 no now playing request on this face
#define OUTBOX_QUEUE_MAX   (6 + OUTBOX_PHONE_SLOTS + OUTBOX_MUSIC_SLOTS)
 Most jobs the work queue or the failed set holds at once.

Typedefs

typedef void(*) WeatherHandler(const WeatherMessage *msg)
 Called with one weather message, holding every group of readings it carried.
typedef void(*) CoordsHandler(const char *lat, const char *lon)
 Called with a fresh pair of coordinates.
typedef void(*) SettingsChangedHandler(bool time_or_date_changed)
 Called after a settings save that changed something.
typedef void(*) UnitChangedHandler(bool fahrenheit)
 Called when the wearer switches the temperature unit on the settings page.
typedef void(*) StockStripHandler(const uint8_t *buf, uint16_t len)
 Called with a packed watchlist strip. The handler owns the wire format.
typedef void(*) CalendarStripHandler(const uint8_t *buf, uint16_t len)
 Called with a packed agenda strip. The store owns the wire format.
typedef void(*) PhoneBatteryHandler(int level)
 Called with the phone's battery level.
typedef void(*) MusicHandler(const char *title, const char *artist, bool playing)
 Called with what the phone is playing.
typedef void(*) MusicNoPluginHandler(bool no_plugin)
 Called with whether the phone says no plugin on it serves now playing, which it sends as WIRE_NO_PLUGIN in place of playing.
typedef void(*) MusicArtHandler(const uint8_t *buf, uint16_t len)
 Called with the album art the phone sent, as raw wire bytes.
typedef void(*) InboxCompleteHandler(void)
 Called once after every inbox message is fully handled.
typedef void(*) LongTextReceiver(const char *text)
 Called with a long text setting the phone sent, for the face to store.
typedef void(*) LongTextProvider(char *out, size_t size)
 Called to write the long text setting the face holds, for the settings reply and to tell a save that changed it from one that did not.

Enumerations

enum  OutboxKind {
  OUTBOX_NONE , OUTBOX_WEATHER , OUTBOX_SETTINGS , OUTBOX_FRESH ,
  OUTBOX_STOCK , OUTBOX_CALENDAR , OUTBOX_PHONE , OUTBOX_MUSIC
}
 What an outbox job sends. More...

Functions

void appmessage_on_weather (WeatherHandler cb)
 One weather message, every group it carried.
void appmessage_on_coords (CoordsHandler cb)
 Latitude and longitude.
void appmessage_on_settings_changed (SettingsChangedHandler cb)
 A setting changed on the phone.
void appmessage_on_unit_changed (UnitChangedHandler cb)
 The wearer switched the temperature unit.
void appmessage_on_stock_strip (StockStripHandler cb)
 Packed watchlist strip.
void appmessage_on_calendar_strip (CalendarStripHandler cb)
 Packed agenda strip.
void appmessage_on_phone_battery (PhoneBatteryHandler cb)
 The phone's battery. Does nothing on a face without the phone keys.
void appmessage_on_music (MusicHandler cb)
 What the phone is playing. Does nothing on a face without the music keys.
void appmessage_on_music_no_plugin (MusicNoPluginHandler cb)
 Whether no plugin serves now playing, just before each music message. Does nothing on a face without the music keys.
void appmessage_on_music_art (MusicArtHandler cb)
 Album art. Does nothing on a face without the album art key.
void appmessage_set_long_text (uint32_t key, uint16_t cap, LongTextProvider provider, LongTextReceiver receiver)
 Sets up one text setting too long for the settings blob, on a message key of the face's own.
void appmessage_add_inbox_complete (InboxCompleteHandler cb)
 Adds work to run once after a whole inbound message is handled, after every channel in it.
void appmessage_open (uint32_t inbox_size)
 Register the SDK callbacks and open the inbox and outbox.
void appmessage_request_weather (void)
 Asks the phone for a fresh weather reading. The weather store calls this on its turn on the shared cadence. On a face that does not declare the weather keys it sends nothing and logs one warning the first time it is called. The weather keys are WEATHER_REQUEST, WEATHER_TEMPERATURE, WEATHER_CONDITIONS, and WEATHER_OK, and a face that declares only some of them fails to build, with each missing one named.
void appmessage_request_stock (void)
 Asks the phone for fresh quotes. The stock store calls this on its turn on the shared cadence. Does nothing on faces that do not declare the stock keys.
void appmessage_request_calendar (void)
 Asks the phone for a fresh agenda. The calendar store calls this on its turn on the shared cadence. Does nothing on faces that do not declare the calendar keys.
void appmessage_request_phone (void)
 Asks the phone for its battery level. The phone battery store calls this at launch and when the phone comes back. Does nothing on a face that does not declare PHONE_BATTERY and PHONE_REQUEST.
void appmessage_release_phone (void)
 Tells the phone the face no longer shows the phone's battery, so it ends its subscription and stops waking the watch on every change in the level. The phone battery store calls this when a face starts it not live. Goes out as PHONE_REQUEST 0, and does nothing on a face without the keys.
void appmessage_request_music (void)
 Asks the phone what it is playing. The now playing store calls this at launch and when the phone comes back. Does nothing on a face that does not declare the four music keys.
void appmessage_release_music (void)
 Tells the phone the face no longer shows what is playing, so it ends its subscription and stops waking the watch on every change of track. The now playing store calls this when a face starts it not live. Goes out as MUSIC_REQUEST 0, and does nothing on a face without the keys.
bool long_text_set (LongText *slot, uint32_t key, uint16_t cap, LongTextProvider provider, LongTextReceiver receiver, bool opened)
 Sets up the face's long text setting, replacing one set before.
bool long_text_fits (uint32_t text_bytes, uint32_t reply_bytes, uint32_t outbox_max, uint32_t inbox_room)
 Whether the text at its cap fits both buffers: the outbox beside the rest of the settings reply, and the inbox on its own.
void long_text_held (const LongText *slot, char *out)
 Writes the text the face holds, as its provider writes it, ended inside the cap.
bool long_text_arrived (const LongText *slot, const char *text)
 Hands a text that arrived to the receiver when it differs from the one the face holds.
static bool outbox_busy (const OutboxQueue *outbox)
 Whether anything is in flight, which is what says the outbox is busy.
static bool outbox_pending (const OutboxQueue *outbox, OutboxKind kind)
 Whether this kind is already somewhere in the outbox.
static bool outbox_push (OutboxQueue *outbox, OutboxKind kind, int retries)
 Add a job to the work queue unless its kind is already pending.
static void outbox_pop_front (OutboxQueue *outbox)
 Drop the front job off the work queue, shifting the rest forward.
static void outbox_take_head (OutboxQueue *outbox)
 Take the head of the queue as the job now in flight.
static OutboxJob outbox_release (OutboxQueue *outbox)
 Mark whatever was in flight as settled, whichever way it went.
static void outbox_hold_failed (OutboxQueue *outbox, OutboxJob job)
 Hold a nacked job for the next pass, if it still has retries.
static int outbox_retry_pass (OutboxQueue *outbox)
 Move the whole failed set back into the work queue for another pass.
static void outbox_clear (OutboxQueue *outbox)
 Forget everything waiting, for a phone that is no longer there.
int32_t tuple_int_or (const Tuple *tuple, int32_t fallback)
 Reads an int tuple, whatever width the phone sent it at.
const char * tuple_str_or (const Tuple *tuple, const char *fallback)
 Reads a cstring tuple, or the fallback when it is not one that can be read.
int32_t tuple_number_or (const Tuple *tuple, int32_t fallback)
 Reads a whole number whether the phone sent it as an int or as text holding only digits.

Detailed Description

The AppMessage pipeline to the phone and the byte readers behind it.

The stores use the byte readers to unpack the packed strips the phone sends.

Typedef Documentation

◆ CalendarStripHandler

typedef void(*) CalendarStripHandler(const uint8_t *buf, uint16_t len)

Called with a packed agenda strip. The store owns the wire format.

Parameters
bufThe raw wire bytes.
lenHow many bytes there are.

◆ CoordsHandler

typedef void(*) CoordsHandler(const char *lat, const char *lon)

Called with a fresh pair of coordinates.

Parameters
latLatitude, pre-formatted as a dash string like "33-44".
lonLongitude, pre-formatted as a dash string like "-112-07".

◆ InboxCompleteHandler

typedef void(*) InboxCompleteHandler(void)

Called once after every inbox message is fully handled.

Lets a store that took several channels in one push save them together in one write, rather than once per channel.

◆ LongTextProvider

typedef void(*) LongTextProvider(char *out, size_t size)

Called to write the long text setting the face holds, for the settings reply and to tell a save that changed it from one that did not.

Parameters
[out]outThe buffer to fill. Write the text ended, cut short to fit.
sizeThe size of out, which is the cap the face gave.

◆ LongTextReceiver

typedef void(*) LongTextReceiver(const char *text)

Called with a long text setting the phone sent, for the face to store.

The text is read straight off the inbox, so it only lives for this call.

Parameters
textThe text as the phone sent it, ended.

◆ MusicArtHandler

typedef void(*) MusicArtHandler(const uint8_t *buf, uint16_t len)

Called with the album art the phone sent, as raw wire bytes.

The bytes are read straight off the inbox, so they only live for this call. wire/music_art.h reads them.

Parameters
bufThe art as the phone packed it.
lenHow many bytes there are.

◆ MusicHandler

typedef void(*) MusicHandler(const char *title, const char *artist, bool playing)

Called with what the phone is playing.

The text is read straight off the inbox, so it only lives for this call.

Parameters
titleThe song's title, empty for none.
artistWho it is by, empty for none.
playingTrue while it plays.

◆ MusicNoPluginHandler

typedef void(*) MusicNoPluginHandler(bool no_plugin)

Called with whether the phone says no plugin on it serves now playing, which it sends as WIRE_NO_PLUGIN in place of playing.

It is called for every now playing message, just before the music handler for the same message, so a store can take both in together.

Parameters
no_pluginTrue while no plugin on the phone serves now playing.

◆ PhoneBatteryHandler

typedef void(*) PhoneBatteryHandler(int level)

Called with the phone's battery level.

Parameters
levelThe level from 0 to 100, PHONE_NO_LEVEL when the phone has no reading yet, or WIRE_NO_PLUGIN while no plugin on the phone serves it.

◆ SettingsChangedHandler

typedef void(*) SettingsChangedHandler(bool time_or_date_changed)

Called after a settings save that changed something.

Parameters
time_or_date_changedTrue when the date or time format changed, so the face should re-render the clock.

◆ StockStripHandler

typedef void(*) StockStripHandler(const uint8_t *buf, uint16_t len)

Called with a packed watchlist strip. The handler owns the wire format.

Parameters
bufThe raw wire bytes.
lenHow many bytes there are.

◆ UnitChangedHandler

typedef void(*) UnitChangedHandler(bool fahrenheit)

Called when the wearer switches the temperature unit on the settings page.

Not called for a restore, which brings the unit any reading already on the watch was fetched in.

Parameters
fahrenheitTrue when the new unit is Fahrenheit.

◆ WeatherHandler

typedef void(*) WeatherHandler(const WeatherMessage *msg)

Called with one weather message, holding every group of readings it carried.

The message is read straight off the inbox, so it only lives for this call. The pipeline does not know the unit label. The face adds "°C" or "°F" from its own setting when it draws.

Parameters
msgThe message. weather_reading_apply turns it into the kept reading.

Enumeration Type Documentation

◆ OutboxKind

enum OutboxKind

What an outbox job sends.

Enumerator
OUTBOX_NONE 

The zero value, so a default-initialized job slot reads as nothing rather than a real request.

OUTBOX_WEATHER 

A weather request.

OUTBOX_SETTINGS 

The settings reply.

OUTBOX_FRESH 

The short settings reply, only whether the watch booted with nothing saved.

OUTBOX_STOCK 

A stock request.

OUTBOX_CALENDAR 

A calendar request.

OUTBOX_PHONE 

A phone battery request.

OUTBOX_MUSIC 

A now playing request.

Function Documentation

◆ appmessage_add_inbox_complete()

void appmessage_add_inbox_complete ( InboxCompleteHandler cb)

Adds work to run once after a whole inbound message is handled, after every channel in it.

Unlike the channel handlers, this adds to a list rather than replacing a handler, so several stores can each commit what one message brought them. Adding the same function twice does nothing.

Parameters
cbThe work to run. NULL is ignored.

◆ appmessage_open()

void appmessage_open ( uint32_t inbox_size)

Register the SDK callbacks and open the inbox and outbox.

Call after settings_init and after the channel handlers are installed. The outbox is sized from the face's settings table, counting every string at its full buffer, so it needs nothing from the face.

The inbox is the face's to size, because the biggest message in is the settings page's save and Clay sends every message key in it, including ones the watch never reads. Add up the longest value each key on the settings page can hold plus 7 bytes of header per key, and leave some room over. A message bigger than the inbox is dropped whole and logged with its reason, so a settings save that never lands is the sign the number is too small.

Parameters
inbox_sizeThe inbox buffer in bytes. Anything over the platform maximum opens at the maximum.

◆ appmessage_set_long_text()

void appmessage_set_long_text ( uint32_t key,
uint16_t cap,
LongTextProvider provider,
LongTextReceiver receiver )

Sets up one text setting too long for the settings blob, on a message key of the face's own.

A settings field lives in a blob of at most 256 bytes, so a face keeps a longer text itself, split over as many persist keys as it needs. The phone sends it on its key with every save. The transport hands it to the receiver only when it differs from what the provider writes, so an unchanged save changes nothing, and adds the provider's text to the settings reply so the settings page reopens on it. A later call replaces the setting.

Call it before appmessage_open, which sizes the outbox to hold the text at its cap. A call after that is logged and refused, and so is one missing the provider or the receiver, or with a cap past LONG_TEXT_CAP_MAX. appmessage_open also logs and leaves out a text whose cap the outbox cannot hold beside the rest of the settings reply, or the inbox cannot hold on its own. The provider writes into a stack buffer of the cap's size for the one send or compare, so nothing holds a worst case buffer between them.

appmessage_set_long_text(MESSAGE_KEY_APPEARANCE_PALETTE, 400, palette_write, palette_store);
void appmessage_set_long_text(uint32_t key, uint16_t cap, LongTextProvider provider, LongTextReceiver receiver)
Sets up one text setting too long for the settings blob, on a message key of the face's own.
Definition appmessage.c:102
void appmessage_open(uint32_t inbox_size)
Register the SDK callbacks and open the inbox and outbox.
Definition appmessage.c:827
Parameters
keyThe MESSAGE_KEY_* value the text rides on, which the face declares.
capThe most bytes the text takes, its end included, up to LONG_TEXT_CAP_MAX.
providerWrites the text the face holds.
receiverStores a text that arrived.

◆ long_text_arrived()

bool long_text_arrived ( const LongText * slot,
const char * text )

Hands a text that arrived to the receiver when it differs from the one the face holds.

The phone sends the text with every save, so taking each arrival as a change would make every save rebuild the face and refetch whatever follows a settings change. Only as much as the cap holds is compared, so a longer text whose held part matches does not count as a change on every save.

Parameters
slotThe setting.
textThe text the message carries, ended.
Returns
True when it went to the receiver, so the save changed something.

◆ long_text_fits()

bool long_text_fits ( uint32_t text_bytes,
uint32_t reply_bytes,
uint32_t outbox_max,
uint32_t inbox_room )

Whether the text at its cap fits both buffers: the outbox beside the rest of the settings reply, and the inbox on its own.

A settings reply that does not fit the outbox goes out empty every time, and a save holding a text the inbox cannot take is dropped whole, so a cap either buffer cannot hold would lose every reply or every save.

Parameters
text_bytesWhat the text takes in a message at its cap, its header included.
reply_bytesWhat the settings reply takes without the text, the dictionary's header included.
outbox_maxThe most the outbox can open at.
inbox_roomWhat the inbox holds past the dictionary's own header.
Returns
True when both hold it.

◆ long_text_held()

void long_text_held ( const LongText * slot,
char * out )

Writes the text the face holds, as its provider writes it, ended inside the cap.

Parameters
slotThe setting, which has to hold one.
[out]outA buffer of the cap's size.

◆ long_text_set()

bool long_text_set ( LongText * slot,
uint32_t key,
uint16_t cap,
LongTextProvider provider,
LongTextReceiver receiver,
bool opened )

Sets up the face's long text setting, replacing one set before.

It has to come before the transport opens, since the outbox is sized then and has to hold the text at its cap. It needs both a provider and a receiver, since without the provider every save would count as a change, and without the receiver nothing would keep what the page sends. A set after the transport opened, one with a cap of 0 or past LONG_TEXT_CAP_MAX, or one missing either function is logged and refused, and whatever was set before stays.

Parameters
slotThe setting to fill.
keyThe MESSAGE_KEY_* value the text rides on.
capThe most bytes the text takes, its end included.
providerWrites the text the face holds.
receiverStores a text that arrived.
openedWhether the transport has opened already.
Returns
True when the setting was taken.

◆ outbox_busy()

bool outbox_busy ( const OutboxQueue * outbox)
inlinestatic

Whether anything is in flight, which is what says the outbox is busy.

Parameters
outboxThe outbox to read.
Returns
True while a send is waiting on its sent or failed callback.

◆ outbox_clear()

void outbox_clear ( OutboxQueue * outbox)
inlinestatic

Forget everything waiting, for a phone that is no longer there.

Nothing reaches a phone that is not connected, so a store's next poll asks again once it is back rather than a stale queue draining into it.

Parameters
outboxThe outbox to empty.

◆ outbox_hold_failed()

void outbox_hold_failed ( OutboxQueue * outbox,
OutboxJob job )
inlinestatic

Hold a nacked job for the next pass, if it still has retries.

Parameters
outboxThe outbox to hold it in.
jobThe job that just nacked.

◆ outbox_pending()

bool outbox_pending ( const OutboxQueue * outbox,
OutboxKind kind )
inlinestatic

Whether this kind is already somewhere in the outbox.

Covers all three places a job can be, so a poll that repeats while an earlier one is still working its way out does not queue a second copy of the same request.

Parameters
outboxThe outbox to read.
kindThe job kind to look for.
Returns
True when that kind is in flight, queued, or held after a nack.

◆ outbox_pop_front()

void outbox_pop_front ( OutboxQueue * outbox)
inlinestatic

Drop the front job off the work queue, shifting the rest forward.

Parameters
outboxThe outbox to drop from.

◆ outbox_push()

bool outbox_push ( OutboxQueue * outbox,
OutboxKind kind,
int retries )
inlinestatic

Add a job to the work queue unless its kind is already pending.

Parameters
outboxThe outbox to add to.
kindThe job kind to send.
retriesRetry passes left if the first send nacks.
Returns
True when the job was added, false when it was already pending or the queue is full.

◆ outbox_release()

OutboxJob outbox_release ( OutboxQueue * outbox)
inlinestatic

Mark whatever was in flight as settled, whichever way it went.

Parameters
outboxThe outbox to clear.
Returns
The job that was in flight, so a nacked one can be handed to outbox_hold_failed.

◆ outbox_retry_pass()

int outbox_retry_pass ( OutboxQueue * outbox)
inlinestatic

Move the whole failed set back into the work queue for another pass.

Parameters
outboxThe outbox to move within.
Returns
How many jobs were moved back.

◆ outbox_take_head()

void outbox_take_head ( OutboxQueue * outbox)
inlinestatic

Take the head of the queue as the job now in flight.

The caller sends it first and only takes it once the send was accepted, so a refused send leaves the head where it was.

Parameters
outboxThe outbox to take from.

◆ tuple_int_or()

int32_t tuple_int_or ( const Tuple * tuple,
int32_t fallback )

Reads an int tuple, whatever width the phone sent it at.

A sender may pick TUPLE_INT or TUPLE_UINT for an integer, so both are accepted and the choice only decides what the top bit means.

Parameters
tupleThe tuple, or NULL when the key was absent.
fallbackReturned when the tuple is absent, is not an int, or carries a width no sender may use.
Returns
The value, or fallback.

◆ tuple_number_or()

int32_t tuple_number_or ( const Tuple * tuple,
int32_t fallback )

Reads a whole number whether the phone sent it as an int or as text holding only digits.

Clay sends a select as text and a toggle as an int, and a face's own component can send either, so every toggle and choice reads through here and takes the value whichever way it came. An int reads as tuple_int_or reads it. Text reads through wire_digits_or, so a sign, a space, or anything after the digits gives the fallback, and digits too big for an int32_t read as INT32_MAX. A colour stays on tuple_int_or, since its text could be hex and this would read it as decimal.

Parameters
tupleThe tuple, or NULL when the key was absent.
fallbackReturned when the tuple is absent, or is neither an int nor digits.
Returns
The value, or fallback.

◆ tuple_str_or()

const char * tuple_str_or ( const Tuple * tuple,
const char * fallback )

Reads a cstring tuple, or the fallback when it is not one that can be read.

Parameters
tupleThe tuple, or NULL when the key was absent.
fallbackReturned when the tuple is absent, is not a cstring, or carries no terminator inside its own length.
Returns
The string, or fallback.