#include "libs/http_client/curl_thread_pool_p.h" #include "picodeparser.h" #include "pidigest.h" #include "pihttpclient.h" #include "piliterals.h" #include "pimqttclient.h" #include "pip.h" #include "piunits.h" #include "pivaluetree_conversions.h" using namespace PICoutManipulators; using namespace PIHTTP; /* enum class UsagePage { Undefined = 0x00, Generic = 0x01, Simulation = 0x02, VR = 0x03, Sport = 0x04, Game = 0x05, Keyboard = 0x07, Led = 0x08, Button = 0x09, Ordinal = 0x0a, Telephony = 0x0b, Consumer = 0x0c, Digitizer = 0x0d, Unicode = 0x10, AlphaNumeric = 0x14, MicrosoftBluetoothHandsfree = 0xff3, }; enum class UsageGeneric { Pointer = 0x01, Mouse = 0x02, Joystick = 0x04, Gamepad = 0x05, Keyboard = 0x06, Keypad = 0x07, SystemCtl = 0x80, X = 0x30, Y = 0x31, Z = 0x32, Rx = 0x33, Ry = 0x34, Rz = 0x35, Slider = 0x36, Dial = 0x37, Wheel = 0x38, Hatswitch = 0x39, CountedBuffer = 0x3a, ByteCount = 0x3b, MotionWakeup = 0x3c, VX = 0x40, VY = 0x41, VZ = 0x42, Vbrx = 0x43, Vbry = 0x44, Vbrz = 0x45, Vno = 0x46, SysctlPower = 0x81, SysctlSleep = 0x82, SysctlWake = 0x83, SysctlContextMenu = 0x84, SysctlMainMmenu = 0x85, SysctlAppMmenu = 0x86, SysctlHelpMenu = 0x87, SysctlMenuExit = 0x88, SysctlMenuSelect = 0x89, SysctlMenuRight = 0x8a, SysctlMenuLeft = 0x8b, SysctlMenuUp = 0x8c, SysctlMenuDown = 0x8d, }; enum class UsageSimulation { Rudder = 0xba, Throttle = 0xbb, }; enum class UsageKeyboard { Noevent = 0x00, Rollover = 0x01, Postfail = 0x02, Undefined = 0x03, aa = 0x04, zz = 0x1d, One = 0x1e, Zero = 0x27, LCtrl = 0xe0, LShft = 0xe1, LAlt = 0xe2, LGui = 0xe3, Rctrl = 0xe4, Rshft = 0xe5, Ralt = 0xe6, Rgui = 0xe7, ScrollLock = 0x47, NumLock = 0x53, CapsLock = 0x39, F1 = 0x3a, F2 = 0x3b, F3 = 0x3c, F4 = 0x3d, F5 = 0x3e, F6 = 0x3f, F7 = 0x40, F8 = 0x41, F9 = 0x42, F10 = 0x43, F11 = 0x44, F12 = 0x45, Return = 0x28, Escape = 0x29, Delete = 0x2a, PrintScreen = 0x46, }; enum class UsageLED { NumLock = 0x01, CapsLock = 0x02, ScrollLock = 0x03, Compose = 0x04, Kana = 0x05, Power = 0x06, Shift = 0x07, DoNotDisturb = 0x08, Mute = 0x09, ToneEnable = 0x0a, HighCutFilter = 0x0b, LowCutFilter = 0x0c, EqualizerEnable = 0x0d, SoundFieldOn = 0x0e, SurroundFieldOn = 0x0f, Repeat = 0x10, Stereo = 0x11, SamplingRateDetect = 0x12, Spinning = 0x13, Cav = 0x14, Clv = 0x15, RecordingFormatDet = 0x16, OffHook = 0x17, Ring = 0x18, MessageWaiting = 0x19, DataMode = 0x1a, BatteryOperation = 0x1b, BatteryOk = 0x1c, BatteryLow = 0x1d, Speaker = 0x1e, HeadSet = 0x1f, Hold = 0x20, Microphone = 0x21, Coverage = 0x22, NightMode = 0x23, SendCalls = 0x24, CallPickup = 0x25, Conference = 0x26, Standby = 0x27, CameraOn = 0x28, CameraOff = 0x29, Online = 0x2a, Offline = 0x2b, Busy = 0x2c, Ready = 0x2d, PaperOut = 0x2e, PaperJam = 0x2f, Remote = 0x30, Forward = 0x31, Reverse = 0x32, Stop = 0x33, Rewind = 0x34, FastForward = 0x35, Play = 0x36, Pause = 0x37, Record = 0x38, Error = 0x39, SelectedIndicator = 0x3a, InUseIndicator = 0x3b, MultiModeIndicator = 0x3c, IndicatorOn = 0x3d, IndicatorFlash = 0x3e, IndicatorSlowBlink = 0x3f, IndicatorFastBlink = 0x40, IndicatorOff = 0x41, FlashOnTime = 0x42, SlowBlinkOnTime = 0x43, SlowBlinkOffTime = 0x44, FastBlinkOnTime = 0x45, FastBlinkOffTime = 0x46, IndicatorColor = 0x47, Red = 0x48, Green = 0x49, Amber = 0x4a, GenericIndicator = 0x4b, }; enum class UsageTelephony { Phone = 0x01, AnsweringMachine = 0x02, MessageControls = 0x03, Handset = 0x04, Headset = 0x05, Keypad = 0x06, ProgrammableButton = 0x07, Redial = 0x24, Transfer = 0x25, Drop = 0x26, Line = 0x2a, RingEnable = 0x2d, Send = 0x31, Keypad0 = 0xb0, KeypadD = 0xbf, HostAvailable = 0xf1, }; enum class UsageMicrosoftBluetoothHandsfree { DialNumber = 0x21, DialMemory = 0x22, }; enum class UsageConsumer { Ctrl = 0x01, }; enum class UsageDigitizer { Pen = 0x02, InRange = 0x32, TipSwitch = 0x42, BarrelSwitch = 0x44, }; */ // using UsagePage = PIVector>; enum class UsagePageID { Undefined = 0x00, Generic = 0x01, Simulation = 0x02, VR = 0x03, Sport = 0x04, Game = 0x05, Keyboard = 0x07, Led = 0x08, Button = 0x09, Ordinal = 0x0a, Telephony = 0x0b, Consumer = 0x0c, Digitizer = 0x0d, Unicode = 0x10, AlphaNumeric = 0x14, MicrosoftBluetoothHandsfree = 0xff3, }; UsagePage usage_generic = { {"Pointer", 0x01}, {"Mouse", 0x02}, {"Joystick", 0x04}, {"Gamepad", 0x05}, {"Keyboard", 0x06}, {"Keypad", 0x07}, {"SystemCtl", 0x80}, {"X", 0x30}, {"Y", 0x31}, {"Z", 0x32}, {"Rx", 0x33}, {"Ry", 0x34}, {"Rz", 0x35}, {"Slider", 0x36}, {"Dial", 0x37}, {"Wheel", 0x38}, {"Hatswitch", 0x39}, {"CountedBuffer", 0x3a}, {"ByteCount", 0x3b}, {"MotionWakeup", 0x3c}, {"VX", 0x40}, {"VY", 0x41}, {"VZ", 0x42}, {"Vbrx", 0x43}, {"Vbry", 0x44}, {"Vbrz", 0x45}, {"Vno", 0x46}, {"SysctlPower", 0x81}, {"SysctlSleep", 0x82}, {"SysctlWake", 0x83}, {"SysctlContextMenu", 0x84}, {"SysctlMainMmenu", 0x85}, {"SysctlAppMmenu", 0x86}, {"SysctlHelpMenu", 0x87}, {"SysctlMenuExit", 0x88}, {"SysctlMenuSelect", 0x89}, {"SysctlMenuRight", 0x8a}, {"SysctlMenuLeft", 0x8b}, {"SysctlMenuUp", 0x8c}, {"SysctlMenuDown", 0x8d}, }; UsagePage usage_simulation = { {"Rudder", 0xba}, {"Throttle", 0xbb}, }; UsagePage usage_keyboard = { {"NoEvent", 0x00}, {"Rollover", 0x01}, {"Postfail", 0x02}, {"Undefined", 0x03}, {"a", 0x04}, {"b", 0x05}, {"c", 0x06}, {"d", 0x07}, {"e", 0x08}, {"f", 0x09}, {"g", 0x0a}, {"h", 0x0b}, {"i", 0x0c}, {"j", 0x0d}, {"k", 0x0e}, {"l", 0x0f}, {"m", 0x10}, {"n", 0x11}, {"o", 0x12}, {"p", 0x13}, {"q", 0x14}, {"r", 0x15}, {"s", 0x16}, {"t", 0x17}, {"u", 0x18}, {"v", 0x19}, {"w", 0x1a}, {"x", 0x1b}, {"y", 0x1c}, {"z", 0x1d}, {"One", 0x1e}, {"Zero", 0x27}, {"LCtrl", 0xe0}, {"LShft", 0xe1}, {"LAlt", 0xe2}, {"LGui", 0xe3}, {"Rctrl", 0xe4}, {"Rshft", 0xe5}, {"Ralt", 0xe6}, {"Rgui", 0xe7}, {"ScrollLock", 0x47}, {"NumLock", 0x53}, {"CapsLock", 0x39}, {"F1", 0x3a}, {"F2", 0x3b}, {"F3", 0x3c}, {"F4", 0x3d}, {"F5", 0x3e}, {"F6", 0x3f}, {"F7", 0x40}, {"F8", 0x41}, {"F9", 0x42}, {"F10", 0x43}, {"F11", 0x44}, {"F12", 0x45}, {"Return", 0x28}, {"Escape", 0x29}, {"Delete", 0x2a}, {"PrintScreen", 0x46}, }; UsagePage usage_LED = { {"NumLock", 0x01}, {"CapsLock", 0x02}, {"ScrollLock", 0x03}, {"Compose", 0x04}, {"Kana", 0x05}, {"Power", 0x06}, {"Shift", 0x07}, {"DoNotDisturb", 0x08}, {"Mute", 0x09}, {"ToneEnable", 0x0a}, {"HighCutFilter", 0x0b}, {"LowCutFilter", 0x0c}, {"EqualizerEnable", 0x0d}, {"SoundFieldOn", 0x0e}, {"SurroundFieldOn", 0x0f}, {"Repeat", 0x10}, {"Stereo", 0x11}, {"SamplingRateDetect", 0x12}, {"Spinning", 0x13}, {"Cav", 0x14}, {"Clv", 0x15}, {"RecordingFormatDet", 0x16}, {"OffHook", 0x17}, {"Ring", 0x18}, {"MessageWaiting", 0x19}, {"DataMode", 0x1a}, {"BatteryOperation", 0x1b}, {"BatteryOk", 0x1c}, {"BatteryLow", 0x1d}, {"Speaker", 0x1e}, {"HeadSet", 0x1f}, {"Hold", 0x20}, {"Microphone", 0x21}, {"Coverage", 0x22}, {"NightMode", 0x23}, {"SendCalls", 0x24}, {"CallPickup", 0x25}, {"Conference", 0x26}, {"Standby", 0x27}, {"CameraOn", 0x28}, {"CameraOff", 0x29}, {"Online", 0x2a}, {"Offline", 0x2b}, {"Busy", 0x2c}, {"Ready", 0x2d}, {"PaperOut", 0x2e}, {"PaperJam", 0x2f}, {"Remote", 0x30}, {"Forward", 0x31}, {"Reverse", 0x32}, {"Stop", 0x33}, {"Rewind", 0x34}, {"FastForward", 0x35}, {"Play", 0x36}, {"Pause", 0x37}, {"Record", 0x38}, {"Error", 0x39}, {"SelectedIndicator", 0x3a}, {"InUseIndicator", 0x3b}, {"MultiModeIndicator", 0x3c}, {"IndicatorOn", 0x3d}, {"IndicatorFlash", 0x3e}, {"IndicatorSlowBlink", 0x3f}, {"IndicatorFastBlink", 0x40}, {"IndicatorOff", 0x41}, {"FlashOnTime", 0x42}, {"SlowBlinkOnTime", 0x43}, {"SlowBlinkOffTime", 0x44}, {"FastBlinkOnTime", 0x45}, {"FastBlinkOffTime", 0x46}, {"IndicatorColor", 0x47}, {"Red", 0x48}, {"Green", 0x49}, {"Amber", 0x4a}, {"GenericIndicator", 0x4b}, }; UsagePage usage_telephony = { {"Phone", 0x01}, {"AnsweringMachine", 0x02}, {"MessageControls", 0x03}, {"Handset", 0x04}, {"Headset", 0x05}, {"Keypad", 0x06}, {"ProgrammableButton", 0x07}, {"Redial", 0x24}, {"Transfer", 0x25}, {"Drop", 0x26}, {"Line", 0x2a}, {"RingEnable", 0x2d}, {"Send", 0x31}, {"Keypad0", 0xb0}, {"KeypadD", 0xbf}, {"HostAvailable", 0xf1}, }; UsagePage usage_MicrosoftBluetoothHandsfree = { {"DialNumber", 0x21}, {"DialMemory", 0x22}, }; UsagePage usage_consumer = { {"Ctrl", 0x01}, }; UsagePage usage_digitizer = { {"Pen", 0x02}, {"InRange", 0x32}, {"TipSwitch", 0x42}, {"BarrelSwitch", 0x44}, }; // PIKbdListener kbd; MessageMutable createMessage(Code c, const char * path, const MessageConst & msg) { piCout << "path" << path << "args" << msg.pathArguments(); return MessageMutable().setCode(c); }; int main(int argc, char * argv[]) { PIMap usage_map; // [page] usage_map[(uint16_t)UsagePageID::Generic] = usage_generic; usage_map[(uint16_t)UsagePageID::Simulation] = usage_simulation; usage_map[(uint16_t)UsagePageID::Keyboard] = usage_keyboard; usage_map[(uint16_t)UsagePageID::Led] = usage_LED; usage_map[(uint16_t)UsagePageID::Button] = usage_keyboard; usage_map[(uint16_t)UsagePageID::Telephony] = usage_telephony; usage_map[(uint16_t)UsagePageID::Consumer] = usage_consumer; usage_map[(uint16_t)UsagePageID::Digitizer] = usage_digitizer; usage_map[(uint16_t)UsagePageID::MicrosoftBluetoothHandsfree] = usage_MicrosoftBluetoothHandsfree; auto getName = [&usage_map](const PIHIDeviceInfo::ValueInfoBase & i) -> PIString { if (usage_map.contains(i.usage_page_id)) { const auto & uv(usage_map[i.usage_page_id]); for (const auto & u: uv) { if (u.second == i.usage_id) return u.first; } } return "v%1"_a.arg(i.index + 1); }; auto info = PIHIDevice::findDevice("Wireless"); for (const auto & a: info.axes) { piCout << " Axis" << a.index << a.data_index << getName(a); } for (const auto & b: info.buttons) { piCout << "Button" << b.index << b.data_index << b.code << getName(b); } PIHIDevice hid; if (hid.open(info)) { hid.start(); CONNECTL(&hid, event, [getName](PIHIDevice::Event e) { if (e.type == PIHIDevice::Event::tButton) { piCout << " Axis" << e.button.index << e.button.usage_id << getName(e.button) << "->" << e.value; } else { piCout << "Button" << e.axis.index << e.axis.usage_id << getName(e.axis) << "->" << e.value; } }); piSleep(30); hid.stopAndWait(); } return 0; // piCout << "start ..."; // PIHTTPServer server; // server.registerUnhandled([](const MessageConst & msg) { return createMessage(Code::BadRequest, "unhadled", msg); }); // server.registerPath("api/v1/status", Method::Get, [](const MessageConst & msg) { // return createMessage(Code::Accepted, "api/v1/status", msg); // }); // server.registerPath("api/v1/plugins", Method::Get, [](const MessageConst & msg) { // return createMessage(Code::Accepted, "api/v1/plugins", msg); // }); // server.registerPath("api/v1/task-status", Method::Get, [](const MessageConst & msg) { // return createMessage(Code::Accepted, "api/v1/task-status", msg); // }); // server.registerPath("api/v1/task/{taskID}/status", Method::Get, [](const MessageConst & msg) { // return createMessage(Code::Accepted, "api/v1/task/{taskID}/status", msg); // }); // server.registerPath("api/v1/bort/list", Method::Get, [](const MessageConst & msg) { // return createMessage(Code::Accepted, "api/v1/bort/list", msg); // }); // server.registerPath("api/v1/all", Method::Get, [](const MessageConst & msg) { // return createMessage(Code::Accepted, "api/v1/all", msg); // }); // server.registerPath("api/v1/all/bort{A}/f", Method::Get, [](const MessageConst & msg) { // return createMessage(Code::Accepted, "api/v1/all/*/f", msg); // }); // server.registerPath("api/v1/all2/**", Method::Get, [](const MessageConst & msg) { // return createMessage(Code::Accepted, "api/v1/all2/**", msg); // }); // server.listenAll(12345); // kbd.enableExitCapture('Q'); // WAIT_FOR_EXIT // piCout << "exiting ..."; // server.stop(); // return 0; // PISystemMonitor mon; // mon.startOnSelf(); // PISystemMonitor::totalRAM(); // 2_s .sleep(); // return 0; PIMQTT::Client cl; cl.setConnectTimeout(2_s); cl.subscribe("api/v1/all/bort{A}/f", [](const PIMQTT::MessageConst & msg) { piCout << "1" << msg.topicList() << msg.pathArguments() << msg.body().size(); }); cl.subscribe("api/v1/all/task{T}/f", [](const PIMQTT::MessageConst & msg) { piCout << "2" << msg.topicList() << msg.pathArguments() << msg.body().size(); }); cl.subscribe("api/v1/all/*/f", [](const PIMQTT::MessageConst & msg) { piCout << "3" << msg.topicList() << msg.pathArguments() << msg.body().size(); }); cl.subscribe("api/v1/all2/**", [](const PIMQTT::MessageConst & msg) { piCout << "4" << msg.topicList() << msg.pathArguments() << msg.body().size(); }); CONNECTL(&cl, connected, [&cl] { piCout << "connected"; // cl.subscribe("api/v1/plugins"); // cl.subscribe("api/v1/task-status"); // cl.subscribe("api/v1/*/{taskID}/status"); // cl.subscribe("api/v1/bort/list"); // cl.subscribe("api/v1/all"); // cl.subscribe("/zigbee2mqtt"); // cl.subscribe("/zigbee2mqtt/+/status/"); // cl.subscribe("/zigbee2mqtt/*/status/"); // cl.subscribe("test/#"); // cl.subscribe("#"); // cl.unsubscribe("/zigbee2mqtt"); // cl.unsubscribe("api/v1/all/bort{A}/f"); // cl.unsubscribe("api/v1/all/*/f"); // cl.publish("/zigbee2mqtt/abc", "hello from PIP"_a.toAscii()); }); CONNECTL(&cl, disconnected, [&cl](PIMQTT::Error code) { piCout << "disconnected code" << (int)code; cl.connect("localhost", "PIP"); }); CONNECTL(&cl, receivedUnhandled, [](const PIMQTT::MessageConst & message) { piCout << "receivedUnhandled" << message.topic() << message.pathArguments() << message.payload().size(); }); cl.connect("localhost", "PIP"); piSleep(6.); cl.unsubscribeAll(); kbd.enableExitCapture('Q'); WAIT_FOR_EXIT piCout << "exiting ..."; }