From 100a7cf44c6af081a9c6dd6cf4abbc7f2ad14fd1 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Wed, 30 Nov 2016 15:15:12 +0000 Subject: Detailled comments on can_hvac_values array Change-Id: I34c6712d2cae736a8a883c8335b16325ce2a5871 Signed-off-by: Romain Forlot --- hvac-hybrid-qml-binding.c | 81 +++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 49 deletions(-) (limited to 'hvac-hybrid-qml-binding.c') diff --git a/hvac-hybrid-qml-binding.c b/hvac-hybrid-qml-binding.c index e31af1c..94b3269 100644 --- a/hvac-hybrid-qml-binding.c +++ b/hvac-hybrid-qml-binding.c @@ -47,14 +47,14 @@ static char *can_hvac_components[8] = { // Initialize CAN hvac array that will be sent trough the socket static __u8 can_hvac_values[8] = { - 21, - 21, - 21, - 240, // Don't know why 240 but it was 0xF0 in the original amb hvacplugin - 0, - 1, // Don't know why 1 but it was 0xF0 in the original amb hvacplugin - 0, // Don't know why 0 but it was 0xF0 in the original amb hvacplugin - 0 // Don't know why 0 but it was 0xF0 in the original amb hvacplugin + 21, // LeftTemperature + 21, // RightTemperature + 21, // AverageTemperature + 240, // Don't know why 240 but it was 0xF0 in the original amb hvacplugin + 0, // FanSpeed + 1, // Don't know why 1 but it was 0x01 in the original amb hvacplugin + 0, // Don't know why 0 but it was 0x00 in the original amb hvacplugin + 0 // Don't know why 0 but it was 0x00 in the original amb hvacplugin }; struct can_handler { @@ -74,54 +74,41 @@ static struct can_handler can_handler; /*****************************************************************************************/ /*****************************************************************************************/ -static struct can_handler open_can_dev() +static int open_can_dev() { - struct can_handler ch; - ch.socket = -1; + struct ifreq ifr; - ch.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW); - if (ch.socket < 0) + can_handler.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW); + if (can_handler.socket < 0) { ERROR(interface, "socket could not be created"); } else { - struct ifreq ifr; - // Attemts to open a socket to CAN bus strcpy(ifr.ifr_name, CAN_DEV); - if(ioctl(ch.socket, SIOCGIFINDEX, &ifr) < 0) + if(ioctl(can_handler.socket, SIOCGIFINDEX, &ifr) < 0) { - close(ch.socket); - ch.socket = -1; ERROR(interface, "ioctl failed"); } else { - ch.txAddress.can_family = AF_CAN; - ch.txAddress.can_ifindex = ifr.ifr_ifindex; + can_handler.txAddress.can_family = AF_CAN; + can_handler.txAddress.can_ifindex = ifr.ifr_ifindex; - // And bind it to ch.txAddress struct - if (bind(ch.socket, (struct sockaddr *)&ch.txAddress, sizeof(ch.txAddress)) < 0) + // And bind it to txAddress + if (bind(can_handler.socket, (struct sockaddr *)&can_handler.txAddress, sizeof(can_handler.txAddress)) < 0) { - close(ch.socket); - ch.socket = -1; ERROR(interface, "bind failed"); } - return ch; + else { + return 0; + } } + close(can_handler.socket); + can_handler.socket = -1; } - ERROR(interface, "unexpected behavior"); - return ch; -} - -static void close_can_dev(struct can_handler ch) -{ - int socketId = ch.socket; - if(socketId >= 0) - { - close(socketId); - } + return -1; } // Get original get temperature function from cpp hvacplugin code @@ -129,42 +116,39 @@ static uint8_t get_temperature(uint8_t value) { uint8_t result = ((0xF0 - 0x10) / 15) * value - 16; if (result < 0x10) - result = 0x10; + result = 0x10; if (result > 0xF0) - result = 0xF0; + result = 0xF0; return result; } -static void write_can(struct can_handler ch) +static void write_can() { // Hardcoded can_id and dlc (data lenght code) struct can_frame txCanFrame; txCanFrame.can_id = 0x30; txCanFrame.can_dlc = 8; - if (ch.socket >= 0) + if (can_handler.socket >= 0) { txCanFrame.data[0] = get_temperature(can_hvac_values[0]); txCanFrame.data[1] = get_temperature(can_hvac_values[1]); - txCanFrame.data[2] = get_temperature((can_hvac_values[0] + can_hvac_values[1]) & 0x02); + txCanFrame.data[2] = get_temperature((can_hvac_values[0] + can_hvac_values[1]) / 2); txCanFrame.data[3] = can_hvac_values[3]; txCanFrame.data[4] = can_hvac_values[4]; txCanFrame.data[5] = can_hvac_values[5]; txCanFrame.data[6] = can_hvac_values[6]; txCanFrame.data[7] = can_hvac_values[7]; - sendto(ch.socket, &txCanFrame, sizeof(struct can_frame), 0, - (struct sockaddr*)&ch.txAddress, sizeof(ch.txAddress)); - return; + sendto(can_handler.socket, &txCanFrame, sizeof(struct can_frame), 0, + (struct sockaddr*)&can_handler.txAddress, sizeof(can_handler.txAddress)); } else { ERROR(interface, "socket not initialized"); - return; } - ERROR(interface, "sending on CAN bus. Unexpected behavior"); } static __u8 read_temp_left_zone() @@ -321,7 +305,7 @@ static void set(struct afb_req request) json_object_iter_next(&iter); } - write_can(can_handler); + write_can(); afb_req_success(request, query, "HVAC settings updated"); } @@ -355,7 +339,6 @@ const struct afb_binding *afbBindingV1Register (const struct afb_binding_interfa int afbBindingV1ServiceInit(struct afb_service service) { - can_handler = open_can_dev(); - return can_handler.socket >= 0 ? 0 : -1; + return open_can_dev(); } -- cgit 1.2.3-korg