Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions libpebble2/util/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,16 @@ def _get_prefixes(self):
if self._prefixes is None:
if self.platform is None:
self._prefixes = self.PLATFORM_PATHS['unknown']
elif self._is_frozen() or (not self.any_fit and self.platform.name in self.PLATFORM_PATHS):
# What the platform has always taken. Firmware of the frozen ones takes nothing else.
elif self._is_frozen():
# What the platform has always taken: its firmware takes nothing else.
self._prefixes = self.PLATFORM_PATHS[self.platform.name]
elif not self.any_fit and self.platform.name in self.PLATFORM_PATHS:
# What the platform has always taken, and after its own binary the flexible ones, which
# are built for every watch like it.
always = self.PLATFORM_PATHS[self.platform.name]
flexible = {d: p for d, p in self.get_platforms().items() if p.flexible}
self._prefixes = (always[:1] + tuple(compatible_directories(self.platform, flexible))
+ always[1:])
else:
self._prefixes = tuple(compatible_directories(self.platform, self.get_platforms()))
return self._prefixes
Expand Down Expand Up @@ -159,9 +166,11 @@ def should_permit_install(self):
if self.any_fit:
return True
if platform not in self.PLATFORM_PATHS:
# A platform that only the watch has described: its own binary, or one for its very display.
# A platform that only the watch has described: its own binary, one for its very display, or
# a flexible one of its colour depth.
built_for = self.get_platforms()[prefix]
return built_for.name == platform or built_for.same_display(self.platform)
return (built_for.name == platform or built_for.same_display(self.platform)
or (built_for.flexible and built_for.color == self.platform.color))
if platform in self.MAX_COMPATIBILITY_VERSIONS:
max_version = self.MAX_COMPATIBILITY_VERSIONS[platform].get(prefix, None)
if max_version is None:
Expand Down
43 changes: 35 additions & 8 deletions libpebble2/util/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class Platform(object):
:type abi: str
:param features: Names of what the watch has, e.g. ``"microphone"``.
:type features: tuple
:param flexible: Whether this is a binary built for every display of its shape and colour
depth. It has no ``width`` and ``height``.
:type flexible: bool
:param requires: Names of the features a flexible binary cannot run without.
:type requires: tuple
"""
FEATURES = ("microphone", "smartstrap", "smartstrap_power", "health", "compass", "touch",
"rgb_backlight", "speaker")
Expand All @@ -39,7 +44,7 @@ class Platform(object):
_WATCH_INFO = struct.Struct('>BBBBHHIHBB16s')

def __init__(self, name, width, height, shape="rect", color=False, app_memory=0x10000,
abi="armv7m", features=()):
abi="armv7m", features=(), flexible=False, requires=()):
self.name = name
self.width = width
self.height = height
Expand All @@ -48,13 +53,20 @@ def __init__(self, name, width, height, shape="rect", color=False, app_memory=0x
self.app_memory = app_memory
self.abi = abi
self.features = tuple(features)
self.flexible = flexible
self.requires = tuple(requires)

@classmethod
def from_manifest(cls, platform):
"""The platform a pbw directory's manifest.json describes, or ``None`` if it does not
describe one that can be read."""
try:
display = platform['display']
if platform.get('flexible') is True:
return cls(str(platform['name']), None, None, str(display['shape']),
bool(display['color']), int(platform['limits']['app_memory']),
str(platform['abi']), flexible=True,
requires=[str(f) for f in platform.get('requires', ())])
return cls(str(platform['name']), int(display['width']), int(display['height']),
str(display['shape']), bool(display['color']),
int(platform['limits']['app_memory']), str(platform['abi']),
Expand Down Expand Up @@ -87,15 +99,21 @@ def from_watch(cls, capabilities, info):
def fits(self, watch):
"""Whether a watch of the given platform runs a binary built for this one: the same ABI
and shape, a display no larger than the watch's, no colour on a black and white display
and no more app memory than the watch gives."""
return (self.abi == watch.abi and self.abi in self.ABIS.values()
and no more app memory than the watch gives. A flexible binary has no display size, and
the watch has to have every feature it requires."""
if not (self.abi == watch.abi and self.abi in self.ABIS.values()
and self.shape == watch.shape and self.shape in self.SHAPES.values()
and self.width <= watch.width and self.height <= watch.height
and (watch.color or not self.color)
and self.app_memory <= watch.app_memory)
and self.app_memory <= watch.app_memory):
return False
if self.flexible:
# A feature this library does not know is one the watch does not have.
return set(self.requires) <= set(watch.features)
return self.width <= watch.width and self.height <= watch.height

def same_display(self, other):
return ((self.width, self.height, self.shape, self.color)
return (not self.flexible and not other.flexible
and (self.width, self.height, self.shape, self.color)
== (other.width, other.height, other.shape, other.color))

def __eq__(self, other):
Expand All @@ -105,6 +123,10 @@ def __ne__(self, other):
return not self == other

def __repr__(self):
if self.flexible:
return "Platform({!r}, flexible, {}, {}, {} bytes, {}, requires {})".format(
self.name, self.shape, 'color' if self.color else 'bw', self.app_memory, self.abi,
list(self.requires))
return "Platform({!r}, {}x{}, {}, {}, {} bytes, {})".format(
self.name, self.width, self.height, self.shape, 'color' if self.color else 'bw',
self.app_memory, self.abi)
Expand Down Expand Up @@ -136,7 +158,8 @@ def platform_for_hardware(hardware):
def compatible_directories(watch, directories):
"""
The directories of a pbw a watch can take an app from, best first: that of its own platform,
then one for its very display, the largest display, its colour depth, the newest platform.
then one for its very display, a flexible binary of its colour depth (of several the one that
requires the most features), the largest display, its colour depth, the newest platform.

:param watch: The watch's platform.
:type watch: Platform
Expand All @@ -147,10 +170,14 @@ def compatible_directories(watch, directories):
"""
def rank(item):
prefix, platform = item
# A flexible binary draws to the whole display.
drawn = watch if platform.flexible else platform
return (
platform.name != watch.name,
not platform.same_display(watch),
-(platform.width * platform.height),
not (platform.flexible and platform.color == watch.color),
-len(platform.requires),
-(drawn.width * drawn.height),
platform.color != watch.color,
# A platform this library has not heard of is newer than those it has.
-(_AGE.index(platform.name) if platform.name in _AGE else len(_AGE)),
Expand Down
83 changes: 81 additions & 2 deletions tests/platform_fit_cases.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": 1,
"about": "Which binaries of a pbw fit a watch, best first. See docs/sdk/platforms.md, 'Which binary a watch runs'. A case names its watch and binaries in 'platforms'; a binary's directory in the pbw is its name.",
"about": "Which binaries of a pbw fit a watch, best first. See docs/sdk/platforms.md, 'Which binary a watch runs'. A case names its watch and binaries in 'platforms'; a binary's directory in the pbw is its name. A flexible binary has no display size and lists the features it requires.",
"age": ["aplite", "basalt", "chalk", "diorite", "emery", "flint", "gabbro"],
"platforms": {
"aplite": {"name": "aplite", "abi": "armv7m", "display": {"width": 144, "height": 168, "shape": "rect", "color": false}, "features": ["compass"], "limits": {"app_memory": 24576}},
Expand All @@ -20,7 +20,14 @@
"orb": {"name": "orb", "abi": "armv7m", "display": {"width": 360, "height": 360, "shape": "round", "color": true}, "features": [], "limits": {"app_memory": 131072}},
"chalkboard": {"name": "chalkboard", "abi": "armv7m", "display": {"width": 240, "height": 240, "shape": "rect", "color": false}, "features": [], "limits": {"app_memory": 65536}},
"riscv": {"name": "riscv", "abi": "rv32", "display": {"width": 144, "height": 168, "shape": "rect", "color": false}, "features": [], "limits": {"app_memory": 65536}},
"hex": {"name": "hex", "abi": "armv7m", "display": {"width": 144, "height": 168, "shape": "hexagon", "color": false}, "features": [], "limits": {"app_memory": 65536}}
"hex": {"name": "hex", "abi": "armv7m", "display": {"width": 144, "height": 168, "shape": "hexagon", "color": false}, "features": [], "limits": {"app_memory": 65536}},
"rect-color": {"name": "rect-color", "abi": "armv7m", "flexible": true, "display": {"shape": "rect", "color": true}, "requires": [], "limits": {"app_memory": 65536}},
"rect-color+mic": {"name": "rect-color+mic", "abi": "armv7m", "flexible": true, "display": {"shape": "rect", "color": true}, "requires": ["microphone"], "limits": {"app_memory": 65536}},
"rect-color+mic+health": {"name": "rect-color+mic+health", "abi": "armv7m", "flexible": true, "display": {"shape": "rect", "color": true}, "requires": ["microphone", "health"], "limits": {"app_memory": 65536}},
"rect-color+laser": {"name": "rect-color+laser", "abi": "armv7m", "flexible": true, "display": {"shape": "rect", "color": true}, "requires": ["laser"], "limits": {"app_memory": 65536}},
"rect-color@128k": {"name": "rect-color", "abi": "armv7m", "flexible": true, "display": {"shape": "rect", "color": true}, "requires": [], "limits": {"app_memory": 131072}},
"rect-bw": {"name": "rect-bw", "abi": "armv7m", "flexible": true, "display": {"shape": "rect", "color": false}, "requires": [], "limits": {"app_memory": 65536}},
"round-color": {"name": "round-color", "abi": "armv7m", "flexible": true, "display": {"shape": "round", "color": true}, "requires": [], "limits": {"app_memory": 65536}}
},
"cases": [
{
Expand Down Expand Up @@ -136,6 +143,78 @@
"watch": "slate",
"binaries": ["shale", "granite", "emery"],
"expected": ["granite", "shale", "emery"]
},
{
"name": "A flexible binary of the watch's class beats one for a smaller display",
"watch": "slate",
"binaries": ["emery", "basalt", "rect-color"],
"expected": ["rect-color", "emery", "basalt"]
},
{
"name": "The watch's own platform and its very display come before a flexible binary",
"watch": "slate",
"binaries": ["rect-color", "emery", "granite", "slate"],
"expected": ["slate", "granite", "rect-color", "emery"]
},
{
"name": "Emery takes its own binary before the flexible one",
"watch": "emery",
"binaries": ["rect-color", "basalt", "emery"],
"expected": ["emery", "rect-color", "basalt"]
},
{
"name": "A flexible binary does not fit a watch without a feature it requires",
"watch": "slate",
"binaries": ["rect-color+mic", "rect-color"],
"expected": ["rect-color"]
},
{
"name": "Of the flexible binaries the one that requires the most",
"watch": "emery",
"binaries": ["basalt", "rect-color", "rect-color+mic", "rect-color+mic+health"],
"expected": ["rect-color+mic+health", "rect-color+mic", "rect-color", "basalt"]
},
{
"name": "A required feature the reader does not know is one the watch lacks",
"watch": "emery",
"binaries": ["rect-color+laser", "basalt"],
"expected": ["basalt"]
},
{
"name": "A flexible binary is of one shape",
"watch": "gabbro",
"binaries": ["rect-color", "round-color", "chalk"],
"expected": ["round-color", "chalk"]
},
{
"name": "A new round platform takes the flexible binary before Gabbro's",
"watch": "orb",
"binaries": ["gabbro", "round-color"],
"expected": ["round-color", "gabbro"]
},
{
"name": "A black and white flexible binary fills a colour display, after one of the watch's class",
"watch": "slate",
"binaries": ["flint", "emery", "rect-bw", "rect-color"],
"expected": ["rect-color", "rect-bw", "emery", "flint"]
},
{
"name": "A black and white watch takes no colour flexible binary",
"watch": "paper",
"binaries": ["rect-color", "rect-bw", "flint"],
"expected": ["rect-bw", "flint"]
},
{
"name": "A flexible binary that asks for more app memory than the watch gives does not fit",
"watch": "shale",
"binaries": ["rect-color@128k", "basalt"],
"expected": ["basalt"]
},
{
"name": "A flexible binary that asks for the app memory the watch gives",
"watch": "slate",
"binaries": ["emery", "rect-color@128k"],
"expected": ["rect-color", "emery"]
}
]
}
45 changes: 44 additions & 1 deletion tests/test_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

@pytest.mark.parametrize("case", TABLE['cases'], ids=lambda case: case['name'])
def test_fit_cases(case):
directories = {name + '/': PLATFORMS[name] for name in case['binaries']}
# A directory is named after its platform, which the table may list under another key.
directories = {PLATFORMS[key].name + '/': PLATFORMS[key] for key in case['binaries']}
best = compatible_directories(PLATFORMS[case['watch']], directories)
assert [d.rstrip('/') for d in best] == case['expected']

Expand All @@ -35,6 +36,19 @@ def test_known_platforms_are_the_tables():
assert KNOWN_PLATFORMS[name] == PLATFORMS[name]


def test_flexible_manifest():
flexible = Platform.from_manifest(TABLE['platforms']['rect-color+mic'])
assert flexible.flexible and flexible.requires == ('microphone',)
assert flexible.width is None and flexible.height is None
assert flexible.fits(KNOWN_PLATFORMS['emery'])
assert not flexible.fits(SLATE) # no microphone
assert not flexible.fits(KNOWN_PLATFORMS['gabbro']) # round
assert not flexible.same_display(KNOWN_PLATFORMS['emery'])
# Without the mark a display without a size is one that cannot be read.
unmarked = dict(TABLE['platforms']['rect-color'], flexible=False)
assert Platform.from_manifest(unmarked) is None


def test_root_is_aplite_and_comes_last():
directories = {'': KNOWN_PLATFORMS['aplite'], 'aplite/': KNOWN_PLATFORMS['aplite']}
assert compatible_directories(KNOWN_PLATFORMS['flint'], directories) == ['aplite/', '']
Expand Down Expand Up @@ -137,3 +151,32 @@ def test_unknown_values_fit_nothing():
assert (odd.abi, odd.shape) == ('unknown', 'unknown')
assert odd.features == Platform.FEATURES
assert compatible_directories(odd, {'emery/': KNOWN_PLATFORMS['emery'], 'odd/': odd}) == []


def test_flexible_binaries_in_a_bundle(tmp_path):
table = TABLE['platforms']
path = _pbw(tmp_path, {'rect-color/': table['rect-color'], 'rect-color+mic/': table['rect-color+mic'],
'rect-bw/': table['rect-bw'], 'emery/': None, 'basalt/': None, 'flint/': None})

# A watch takes the binary of its own platform first, then the flexible one that requires the most.
emery = PebbleBundle(path, platform=KNOWN_PLATFORMS['emery'])
assert emery._get_prefixes() == ('emery/', 'rect-color+mic/', 'rect-color/', 'rect-bw/', 'basalt/', '')
assert emery.should_permit_install()
flint = PebbleBundle(path, platform=KNOWN_PLATFORMS['flint'])
assert flint._get_prefixes() == ('flint/', 'rect-bw/', 'diorite/', 'aplite/', '')
# A frozen watch takes what it took.
assert PebbleBundle(path, hardware=PebbleHardware.SNOWY_DVT)._get_prefixes() == ('basalt/', '')

# Slate has no microphone and no binary of its own: the plain flexible one is meant for it.
slate = PebbleBundle(path, platform=SLATE)
assert slate.get_app_path() == 'rect-color/pebble-app.bin'
assert slate.should_permit_install()


def test_flexible_binary_alone(tmp_path):
path = _pbw(tmp_path, {'rect-color+mic/': TABLE['platforms']['rect-color+mic']})
assert PebbleBundle(path, platform=KNOWN_PLATFORMS['emery']).get_app_path() == 'rect-color+mic/pebble-app.bin'
# No microphone, round, black and white: not offered, even when asked for any fit.
for watch in (SLATE, KNOWN_PLATFORMS['gabbro'], KNOWN_PLATFORMS['flint']):
assert PebbleBundle(path, platform=watch, any_fit=True).get_real_path('pebble-app.bin') is None
assert not PebbleBundle(path, platform=watch).should_permit_install()