diff --git a/examples/debug_d4/src/debug.c b/examples/debug_d4/src/debug.c index e6cdcde..5f69e8a 100644 --- a/examples/debug_d4/src/debug.c +++ b/examples/debug_d4/src/debug.c @@ -26,7 +26,7 @@ #include -void test4(uint32_t timestamp, +void test4(uint64_t timestamp, device4_event_type event, uint8_t brightness, const char* msg) { @@ -34,9 +34,6 @@ void test4(uint32_t timestamp, case DEVICE4_EVENT_MESSAGE: printf("Message: `%s`\n", msg); break; - case DEVICE4_EVENT_BRIGHTNESS_SET: - printf("Set Brightness: %u\n", brightness); - break; case DEVICE4_EVENT_BRIGHTNESS_UP: printf("Increase Brightness: %u\n", brightness); break; diff --git a/interface_lib/include/device3.h b/interface_lib/include/device3.h index 6c991ad..d4a3b28 100644 --- a/interface_lib/include/device3.h +++ b/interface_lib/include/device3.h @@ -33,6 +33,15 @@ #include #endif +#define DEVICE3_MSG_GET_CAL_DATA_LENGTH 0x14 +#define DEVICE3_MSG_CAL_DATA_GET_NEXT_SEGMENT 0x15 +#define DEVICE3_MSG_ALLOCATE_CAL_DATA_BUFFER 0x16 +#define DEVICE3_MSG_WRITE_CAL_DATA_SEGMENT 0x17 +#define DEVICE3_MSG_FREE_CAL_BUFFER 0x18 +#define DEVICE3_MSG_START_IMU_DATA 0x19 +#define DEVICE3_MSG_GET_STATIC_ID 0x1A +#define DEVICE3_MSG_UNKNOWN 0x1D + #ifdef __cplusplus extern "C" { #endif diff --git a/interface_lib/include/device4.h b/interface_lib/include/device4.h index 6668efa..1086ad8 100644 --- a/interface_lib/include/device4.h +++ b/interface_lib/include/device4.h @@ -33,7 +33,8 @@ #include #endif -#define DEVICE4_MSG_P_BRIGHTNESS 0x03 +#define DEVICE4_MSG_R_BRIGHTNESS 0x03 + #define DEVICE4_MSG_W_DISP_MODE 0x08 #define DEVICE4_MSG_R_GLASSID 0x15 #define DEVICE4_MSG_R_DP7911_FW_VERSION 0x16 @@ -95,13 +96,12 @@ extern "C" { #endif struct __attribute__((__packed__)) device4_packet_t { - uint8_t signature; + uint8_t head; uint32_t checksum; uint16_t length; - uint8_t _padding0 [4]; - uint32_t timestamp; + uint64_t timestamp; uint16_t msgid; - uint8_t _padding1 [5]; + uint8_t reserved [5]; union { char text [42]; uint8_t data [42]; @@ -112,16 +112,15 @@ enum device4_event_t { DEVICE4_EVENT_UNKNOWN = 0, DEVICE4_EVENT_SCREEN_ON = 1, DEVICE4_EVENT_SCREEN_OFF = 2, - DEVICE4_EVENT_BRIGHTNESS_SET = 3, - DEVICE4_EVENT_BRIGHTNESS_UP = 4, - DEVICE4_EVENT_BRIGHTNESS_DOWN = 5, - DEVICE4_EVENT_MESSAGE = 6, + DEVICE4_EVENT_BRIGHTNESS_UP = 3, + DEVICE4_EVENT_BRIGHTNESS_DOWN = 4, + DEVICE4_EVENT_MESSAGE = 5, }; typedef struct device4_packet_t device4_packet_type; typedef enum device4_event_t device4_event_type; typedef void (*device4_event_callback)( - uint32_t timestamp, + uint64_t timestamp, device4_event_type event, uint8_t brightness, const char* msg @@ -132,6 +131,11 @@ struct device4_t { uint16_t product_id; void* handle; + + bool activated; + char mcu_app_fw_version [42]; + char dp_fw_version [42]; + char dsp_fw_version [42]; bool active; uint8_t brightness; @@ -147,6 +151,8 @@ void device4_clear(device4_type* device); int device4_read(device4_type* device, int timeout); + + void device4_close(device4_type* device); #ifdef __cplusplus diff --git a/interface_lib/src/device3.c b/interface_lib/src/device3.c index 0f3d791..cc31169 100644 --- a/interface_lib/src/device3.c +++ b/interface_lib/src/device3.c @@ -96,15 +96,6 @@ static bool recv_payload(device3_type* device, uint8_t size, uint8_t* payload) { return (transferred == size); } -#define DEVICE3_MSG_GET_CAL_DATA_LENGTH 0x14 -#define DEVICE3_MSG_CAL_DATA_GET_NEXT_SEGMENT 0x15 -#define DEVICE3_MSG_ALLOCATE_CAL_DATA_BUFFER 0x16 -#define DEVICE3_MSG_WRITE_CAL_DATA_SEGMENT 0x17 -#define DEVICE3_MSG_FREE_CAL_BUFFER 0x18 -#define DEVICE3_MSG_START_IMU_DATA 0x19 -#define DEVICE3_MSG_GET_STATIC_ID 0x1A -#define DEVICE3_MSG_UNKNOWN 0x1D - struct __attribute__((__packed__)) device3_payload_packet_t { uint8_t head; uint32_t checksum; diff --git a/interface_lib/src/device4.c b/interface_lib/src/device4.c index 6ad8835..518e68e 100644 --- a/interface_lib/src/device4.c +++ b/interface_lib/src/device4.c @@ -29,12 +29,14 @@ #include #include #include +#include #include #include "crc32.h" #define MAX_PACKET_SIZE 64 +#define PACKET_HEAD 0xFD static bool send_payload(device4_type* device, uint8_t size, const uint8_t* payload) { int payload_size = size; @@ -52,18 +54,41 @@ static bool send_payload(device4_type* device, uint8_t size, const uint8_t* payl return (transferred == size); } +static bool recv_payload(device4_type* device, uint8_t size, uint8_t* payload) { + int payload_size = size; + if (payload_size > MAX_PACKET_SIZE) { + payload_size = MAX_PACKET_SIZE; + } + + int transferred = hid_read(device->handle, payload, payload_size); + + if (transferred >= payload_size) { + transferred = payload_size; + } + + if (transferred == 0) { + return false; + } + + if (transferred != payload_size) { + perror("ERROR: receiving payload failed\n"); + return false; + } + + return (transferred == size); +} + static bool send_payload_action(device4_type* device, uint16_t msgid, uint8_t len, const uint8_t* data) { static device4_packet_type packet; - const uint16_t packet_len = 11 + len; + const uint16_t packet_len = 17 + len; const uint16_t payload_len = 5 + packet_len; - packet.signature = 0xFD; + packet.head = PACKET_HEAD; packet.length = packet_len; - memset(packet._padding0, 0, 4); packet.timestamp = 0; packet.msgid = msgid; - memset(packet._padding1, 0, 5); + memset(packet.reserved, 0, 5); memcpy(packet.data, data, len); packet.checksum = crc32_checksum((const uint8_t*) (&packet.length), packet.length); @@ -71,6 +96,38 @@ static bool send_payload_action(device4_type* device, uint16_t msgid, uint8_t le return send_payload(device, payload_len, (uint8_t*) (&packet)); } +static bool recv_payload_msg(device4_type* device, uint8_t msgid, uint8_t len, uint8_t* data) { + static device4_packet_type packet; + + packet.head = 0; + packet.length = 0; + packet.msgid = 0; + + const uint16_t packet_len = 18 + len; + const uint16_t payload_len = 5 + packet_len; + + if (!recv_payload(device, payload_len, (uint8_t*) (&packet))) { + return false; + } + + if (packet.head != PACKET_HEAD) { + return false; + } + + if (packet.msgid != msgid) { + return false; + } + + const uint8_t status = packet.data[0]; + + if (status != 0) { + return false; + } + + memcpy(data, packet.data + 1, len); + return true; +} + device4_type* device4_open(device4_event_callback callback) { device4_type* device = (device4_type*) malloc(sizeof(device4_type)); @@ -113,20 +170,73 @@ device4_type* device4_open(device4_event_callback callback) { device4_clear(device); - /*if (!send_payload_action(device, DEVICE4_MSG_P_BRIGHTNESS, 0, NULL)) { - perror("Sending brightness command action failed!\n"); + if (!send_payload_action(device, DEVICE4_MSG_R_ACTIVATION_TIME, 0, NULL)) { + perror("Requesting activation time failed!\n"); return device; } - if (0 > device4_read(device, 1000)) { - perror("Reading error!\n"); - }*/ + uint8_t activated; + if (!recv_payload_msg(device, DEVICE4_MSG_R_ACTIVATION_TIME, 1, &activated)) { + perror("Receiving activation time failed!\n"); + return device; + } + + device->activated = (activated != 0); + + if (!device->activated) { + perror("Device is not activated!\n"); + return device; + } + + if (!send_payload_action(device, DEVICE4_MSG_R_MCU_APP_FW_VERSION, 0, NULL)) { + perror("Requesting current MCU app firmware version!\n"); + return device; + } + + if (!recv_payload_msg(device, DEVICE4_MSG_R_MCU_APP_FW_VERSION, 41, (uint8_t*) device->mcu_app_fw_version)) { + perror("Receiving current MCU app firmware version failed!\n"); + return device; + } + + if (!send_payload_action(device, DEVICE4_MSG_R_DP7911_FW_VERSION, 0, NULL)) { + perror("Requesting current DP firmware version!\n"); + return device; + } + + if (!recv_payload_msg(device, DEVICE4_MSG_R_DP7911_FW_VERSION, 41, (uint8_t*) device->dp_fw_version)) { + perror("Receiving current DP firmware version failed!\n"); + return device; + } + + if (!send_payload_action(device, DEVICE4_MSG_R_DSP_APP_FW_VERSION, 0, NULL)) { + perror("Requesting current DSP app firmware version!\n"); + return device; + } + + if (!recv_payload_msg(device, DEVICE4_MSG_R_DSP_APP_FW_VERSION, 41, (uint8_t*) device->dsp_fw_version)) { + perror("Receiving current DSP app firmware version failed!\n"); + return device; + } + + printf("MCU: %s\n", device->mcu_app_fw_version); + printf("DP: %s\n", device->dp_fw_version); + printf("DSP: %s\n", device->dsp_fw_version); + + if (!send_payload_action(device, DEVICE4_MSG_R_BRIGHTNESS, 0, NULL)) { + perror("Requesting initial brightness failed!\n"); + return device; + } + + if (!recv_payload_msg(device, DEVICE4_MSG_R_BRIGHTNESS, 1, &device->brightness)) { + perror("Receiving initial brightness failed!\n"); + return device; + } return device; } static void device4_callback(device4_type* device, - uint32_t timestamp, + uint64_t timestamp, device4_event_type event, uint8_t brightness, const char* msg) { @@ -166,10 +276,15 @@ int device4_read(device4_type* device, int timeout) { } if (MAX_PACKET_SIZE != transferred) { - perror("Not expected issue!\n"); + perror("Reading failed!\n"); return -3; } + if (packet.head != PACKET_HEAD) { + perror("Wrong packet!\n"); + return -4; + } + const uint32_t timestamp = packet.timestamp; const size_t data_len = (size_t) &(packet.data) - (size_t) &(packet.length); @@ -189,20 +304,6 @@ int device4_read(device4_type* device, int timeout) { case DEVICE4_MSG_P_START_HEARTBEAT: { break; } - case DEVICE4_MSG_P_BRIGHTNESS: { - const uint8_t brightness = packet.data[1]; - - device->brightness = brightness; - - device4_callback( - device, - timestamp, - DEVICE4_EVENT_BRIGHTNESS_SET, - device->brightness, - NULL - ); - break; - } case DEVICE4_MSG_P_BUTTON_PRESSED: { const uint8_t phys_button = packet.data[0]; const uint8_t virt_button = packet.data[4]; diff --git a/src/driver.c b/src/driver.c index 82f90d9..28823c1 100644 --- a/src/driver.c +++ b/src/driver.c @@ -74,7 +74,7 @@ void test3(uint64_t timestamp, old = q; } -void test4(uint32_t timestamp, +void test4(uint64_t timestamp, device4_event_type event, uint8_t brightness, const char* msg) { @@ -82,9 +82,6 @@ void test4(uint32_t timestamp, case DEVICE4_EVENT_MESSAGE: printf("Message: `%s`\n", msg); break; - case DEVICE4_EVENT_BRIGHTNESS_SET: - printf("Set Brightness: %u\n", brightness); - break; case DEVICE4_EVENT_BRIGHTNESS_UP: printf("Increase Brightness: %u\n", brightness); break;