{"action":"created","issue":{"url":"https://api.github.com/repos/gartnera/headunit/issues/16","repository_url":"https://api.github.com/repos/gartnera/headunit","labels_url":"https://api.github.com/repos/gartnera/headunit/issues/16/labels{/name}","comments_url":"https://api.github.com/repos/gartnera/headunit/issues/16/comments","events_url":"https://api.github.com/repos/gartnera/headunit/issues/16/events","html_url":"https://github.com/gartnera/headunit/issues/16","id":176272082,"number":16,"title":"Refactor AA protocol code to use protocol buffers","user":{"login":"lmagder","id":2025344,"avatar_url":"https://avatars.githubusercontent.com/u/2025344?v=3","gravatar_id":"","url":"https://api.github.com/users/lmagder","html_url":"https://github.com/lmagder","followers_url":"https://api.github.com/users/lmagder/followers","following_url":"https://api.github.com/users/lmagder/following{/other_user}","gists_url":"https://api.github.com/users/lmagder/gists{/gist_id}","starred_url":"https://api.github.com/users/lmagder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lmagder/subscriptions","organizations_url":"https://api.github.com/users/lmagder/orgs","repos_url":"https://api.github.com/users/lmagder/repos","events_url":"https://api.github.com/users/lmagder/events{/privacy}","received_events_url":"https://api.github.com/users/lmagder/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":14,"created_at":"2016-09-11T23:33:04Z","updated_at":"2016-12-03T15:12:02Z","closed_at":null,"body":"I did some preliminary investigation into writing proto \nfiles instead of manually constructing the wire protocol, but only with some messages sent by the headunit to the phone so far.\n\nFor example in the function I added to send a button message\n\n``` c\nint hu_fill_button_message(uint8_t* buffer, uint64_t timeStamp, HU_INPUT_BUTTON button, int isPress)\n{\n    int buffCount = 0;\n    buffer[buffCount++] = 0x80;\n    buffer[buffCount++] = 0x01;\n    buffer[buffCount++] = 0x08;\n\n    buffCount += varint_encode(timeStamp, buffer + buffCount, 0);\n\n    buffer[buffCount++] = 0x22;\n    buffer[buffCount++] = 0x0A;\n    buffer[buffCount++] = 0x0A;\n    buffer[buffCount++] = 0x08;\n    buffer[buffCount++] = 0x08;\n    buffer[buffCount++] = (uint8_t)button;\n    buffer[buffCount++] = 0x10;\n    buffer[buffCount++] = isPress ? 0x01 : 0x00;\n    buffer[buffCount++] = 0x18;\n    buffer[buffCount++] = 0x00;\n    buffer[buffCount++] = 0x20;\n    buffer[buffCount++] = 0x00;\n    return buffCount;\n}\n```\n\nI saved this out with timestamp = 15, button = 32, isPress = true as buttonMessage.bin. Running this through `protoc --decode_raw < buttonMessage.bin`\nyields\n\n```\n1: 1\n1: 15\n4 {\n  1 {\n    1: 32\n    2: 1\n    3: 0\n    4: 0\n  }\n}\n```\n\nso actually you can see there are two messages concatenated here (since there can't be two field #1s).\nAssuming all the fields are there the first field is pretty simple, something like\n\n```\nmessage Header\n{\n    enum PacketType\n    {\n        INPUT_EVENT = 1;\n        //More here probably\n    }\n    required PacketType type = 1;\n}\n```\n\nso I chopped it off to get \n\n```\nprotoc --decode_raw < buttonMessageNoHeader.bin \n1: 15\n4 {\n  1 {\n    1: 32\n    2: 1\n    3: 0\n    4: 0\n  }\n}\n```\n\nso just making up names assuming what the code does and inferring types from the [wire format](https://developers.google.com/protocol-buffers/docs/encoding) since unfortunately protoc does not print them, despite knowing them.\n\n```\nmessage ButtonInfo\n{\n    required uint32 scanCode = 1;\n    required bool pressed = 2;\n    required uint32 unknown1 = 3 [default = 0];\n    required uint32 unknown2 = 4 [default = 0];\n}\n\nmessage ButtonInfoWrapper\n{\n    required ButtonInfo button = 1;\n}\n\nmessage InputEvent\n{\n    required uint64 timeStamp = 1;\n    optional ButtonInfoWrapper button = 4;\n}\n```\n\nseems to work:\n\n```\nprotoc test.proto --decode HU.InputEvent < buttonMessageNoHeader.bin \ntimeStamp: 15\nbutton {\n  button {\n    scanCode: 32\n    pressed: true\n    unknown1: 0\n    unknown2: 0\n  }\n}\n```\n\nI did a few more like the touch event and the day/night sensor notification and you can start to see patterns. The first packet is always a single enum which is the type of second packet and then all the input events are one packet type with optional sub-structs, same with the sensor event. So it seems you can send all or any subset of the sensors at once. I attached the files.\n\nThis would definitely clean up code a lot (especially the sd_buf stuff in hu_app.c) and it shouldn't add runtime requirements since all the protoc stuff generates code you compile and link against. The only potential problem is that protoc generates C++ code by default, which is probably nicer and would work since input_filter uses modern C++, but would be a drastic change. There is a unofficial protoc-c project though\n[testFiles.zip](https://github.com/gartnera/headunit/files/466285/testFiles.zip)\n"},"comment":{"url":"https://api.github.com/repos/gartnera/headunit/issues/comments/264644737","html_url":"https://github.com/gartnera/headunit/issues/16#issuecomment-264644737","issue_url":"https://api.github.com/repos/gartnera/headunit/issues/16","id":264644737,"user":{"login":"izacus","id":633837,"avatar_url":"https://avatars.githubusercontent.com/u/633837?v=3","gravatar_id":"","url":"https://api.github.com/users/izacus","html_url":"https://github.com/izacus","followers_url":"https://api.github.com/users/izacus/followers","following_url":"https://api.github.com/users/izacus/following{/other_user}","gists_url":"https://api.github.com/users/izacus/gists{/gist_id}","starred_url":"https://api.github.com/users/izacus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/izacus/subscriptions","organizations_url":"https://api.github.com/users/izacus/orgs","repos_url":"https://api.github.com/users/izacus/repos","events_url":"https://api.github.com/users/izacus/events{/privacy}","received_events_url":"https://api.github.com/users/izacus/received_events","type":"User","site_admin":false},"created_at":"2016-12-03T15:12:02Z","updated_at":"2016-12-03T15:12:02Z","body":"Also, some additions about location data:\r\n\r\nlatitude, longtitude seem to be multiplied by 1e7 and then converted to int. Accuracy is multiplied by 1e3, altitude 1e2, speed 1e3 and bearing 1e6. This is probably because all those payloads are ints in protocol, but have decimals in actual data."}}
{"actor":{"display_login":"izacus"}}