DouweM / pebblewatch

A Ruby library for communicating with your Pebble smartwatch.
MIT License
60 stars 8 forks source link

`get_installed_apps` messes up string offsets #2

Open phistep opened 11 years ago

phistep commented 11 years ago

Running following code should show me my installed apps:

require "pebblewatch"
require 'pp'

watch = Pebble::Watch.new("4F92", "/dev/rfcomm0")

watch.connect
pp watch.get_installed_apps
watch.disconnect

prints:

{:banks_count=>8,
 :apps=>
  [{:id=>12,
    :index=>0,
    :name=>"Counter",
    :author=>"Ps0ke",
    :flags=>0,
    :version=>3956},
   {:id=>13,
    :index=>1,
    :name=>"MtG Life Points",
    :author=>"Ps0ke",
    :flags=>0,
    :version=>8392},
   {:id=>14,
    :index=>2,
    :name=>"Dotz",
    :author=>"Zalew",
    :flags=>1,
    :version=>42359},
   {:id=>15,
    :index=>3,
    :name=>"Noms",
    :author=>"Fuzzie",
    :flags=>1,
    :version=>3224},
   {:id=>16,
    :index=>4,
    :name=>"Nyan Watch",
    :author=>"Jeff Pitchell",
    :flags=>1,
    :version=>41153},
   {:id=>0,
    :index=>1347,
    :name=>
     "alendar\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00W",
    :author=>"illiam Heaton",
    :flags=>162,
    :version=>11776},
   {:id=>4608,
    :index=>1607,
    :name=>
     "low\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00d",
    :author=>"otar",
    :flags=>258,
    :version=>21504},
   {:id=>0,
    :index=>478821,
    :name=>
     "olog\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Ps",
    :author=>"0ke",
    :flags=>nil,
    :version=>1}]}

The first ones work well, but starting at ID 0 The Strings get all messed up. The app with ID 0 has the name Calendar, but the first char got lost somewhere. Instead the first char of the author field got at the end of the name field (William). As a result, the nullbytes did not get stripped. My guess is, that the offsets got wrong as you still read 32 byte.

Just for the record: ID 4608's name should be "Glow", the author "dotar" I guess. ID 0 (two identical IDs? Maybe something went wrong here, too?) should have the name "Neolog", and the author is me, "Ps0ke". We're two bytes off by now.

DouweM commented 11 years ago

So evidently the way we're unpacking the installed apps message isn't correct anymore, see lib/pebble/watch.rb#L150.

My Pebble charging cable actually broke a little while ago, so I can't fix and test it myself. I would be very happy to accept a pull request addressing this, though.

phistep commented 11 years ago

The unpack syntax is quite unreadable. I haven't really looked into it, but do you think moving to bindata (now that I pulled it in anyway) makes sense?

Where can I find documentation to the messaging/protocol syntax? Did you just extract it from the Python code?

DouweM commented 11 years ago

The #unpack syntax is just as unreadable as anything you don't know yet ;-) I can see how bindata is useful for larger structures like bundles (which I why I let you pull it in), but for smaller blobs with just a small number of values, my vote goes to #unpack.