First Code

This commit is contained in:
Justin Hammond 2020-06-11 13:14:18 +08:00
parent 7253c379f2
commit 3badf6e5da
26 changed files with 1322 additions and 0 deletions

65
.gitignore vendored Normal file
View file

@ -0,0 +1,65 @@
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
# C++ objects and libs
*.slo
*.lo
*.o
*.a
*.la
*.lai
*.so
*.so.*
*.dll
*.dylib
# Qt-es
object_script.*.Release
object_script.*.Debug
*_plugin_import.cpp
/.qmake.cache
/.qmake.stash
*.pro.user
*.pro.user.*
*.qbs.user
*.qbs.user.*
*.moc
moc_*.cpp
moc_*.h
qrc_*.cpp
ui_*.h
*.qmlc
*.jsc
Makefile*
*build-*
*.qm
*.prl
# Qt unit tests
target_wrapper.*
# QtCreator
*.autosave
# QtCreator Qml
*.qmlproject.user
*.qmlproject.user.*
# QtCreator CMake
CMakeLists.txt.user*
# QtCreator 4.8< compilation database
compile_commands.json
# QtCreator local machine specific files for imported projects
*creator.user*
*_qmlcache.qrc

9
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,9 @@
{
"files.associations": {
"qobject": "cpp",
"array": "cpp",
"string": "cpp",
"string_view": "cpp"
},
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools"
}

17
.vscode/tasks.json vendored Normal file
View file

@ -0,0 +1,17 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Make",
"type": "shell",
"command": "make",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

17
CMakeLists.txt Normal file
View file

@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.1.0)
project(sbcbmc)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
add_subdirectory(plugins)
add_subdirectory(agent)
add_subdirectory(client)

48
agent/CMakeLists.txt Normal file
View file

@ -0,0 +1,48 @@
cmake_minimum_required(VERSION 3.1.0)
project(sbcbmc-agent VERSION 0.0.1)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
find_package(Qt5 COMPONENTS Core REQUIRED)
find_package(Qt5 COMPONENTS RemoteObjects REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LibUSB REQUIRED libusb)
configure_file(include/config.h.in include/config.h)
set(HEADERS
include/daemon.h
include/pluginbase.h
)
set(SOURCES
source/main.cpp
source/daemon.cpp
source/pluginbase.cpp
)
add_executable(sbcbmc-agent
${SOURCES}
${HEADERS}
${REPSOURCES}
)
target_include_directories(sbcbmc-agent PRIVATE
"${PROJECT_BINARY_DIR}"
"${PROJECT_SOURCE_DIR}/include/"
"${CMAKE_SOURCE_DIR}/plugins/thermal/include/"
"${CMAKE_SOURCE_DIR}/plugins/thermal/"
${LibUSB_INCLUDE_DIRS}
)
target_link_libraries(sbcbmc-agent ${LibUSB_LIBRARIES})
target_link_libraries(sbcbmc-agent Qt5::Core Qt5::RemoteObjects)
target_link_libraries(sbcbmc-agent thermal dcdc-usb-200)

View file

@ -0,0 +1,8 @@
#ifndef CONFIG_H
#define CONFIG_H
#define catputer_VERSION_MAJOR @carputer_VERSION_MAJOR@
#define carputer_VERSION_MINOR @carputer_VERSION_MINOR@
#define carputer_VERSION_PATCH @carputer_VERSION_PATCH@
#endif

22
agent/include/daemon.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef DAEMON_H
#define DAEMON_H
#include <QObject>
#include <QString>
#include <QHash>
#include "pluginbase.h"
class daemon : public QObject
{
Q_OBJECT
public:
daemon(QObject *parent = nullptr);
~daemon();
private:
QHash<QString, PluginBase *> m_Plugins;
QRemoteObjectHost *m_rohost;
QRemoteObjectRegistryHost * m_roregistryhost;
};
#endif

View file

@ -0,0 +1,25 @@
#ifndef PLUGINBASE_H
#define PLUGINBASE_H
#include <QObject>
#include <QTimer>
#include <QtRemoteObjects/QRemoteObjectHostBase>
class PluginBase : public QObject {
Q_OBJECT
public:
PluginBase(QObject *parent = nullptr);
~PluginBase();
virtual bool initilize() = 0;
virtual void startTimer();
public Q_SLOTS:
virtual void update() = 0;
virtual bool registerSource(QRemoteObjectHostBase *host) = 0;
private:
QTimer m_updateTimer;
};
#endif

28
agent/source/daemon.cpp Normal file
View file

@ -0,0 +1,28 @@
#include "daemon.h"
#include "dcdc-usb-200.h"
#include "thermal.h"
daemon::daemon(QObject *parent) :
QObject(parent)
{
this->m_roregistryhost = new QRemoteObjectRegistryHost(QUrl("tcp://0.0.0.0:1999"), this);
this->m_rohost = new QRemoteObjectHost(QUrl("tcp://0.0.0.0:1998"), QUrl("tcp://127.0.0.1:1999"), QRemoteObjectHostBase::BuiltInSchemasOnly, this);
DCDCUSB200 *dcdcusb200 = new DCDCUSB200(this);
if (dcdcusb200->initilize()) {
this->m_Plugins.insert("DCDCUSB200", dcdcusb200);
dcdcusb200->registerSource(this->m_rohost);
}
Thermal *thermal = new Thermal(this);
if (thermal->initilize()) {
this->m_Plugins.insert("Thermal", thermal);
thermal->registerSource(this->m_rohost);
}
}
daemon::~daemon()
{
}

34
agent/source/main.cpp Normal file
View file

@ -0,0 +1,34 @@
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QLoggingCategory>
#include "config.h"
#include "daemon.h"
Q_LOGGING_CATEGORY(ozwdaemon, "ozw.daemon");
#define APP_VERSION #carputer_VERSION_MAJOR.#carputer_VERSION_MINOR.#carputer_VERSION_PATCH
#define DEF2STR2(x) #x
#define DEF2STR(x) DEF2STR2(x)
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
QCoreApplication::setApplicationName("carputer");
QCoreApplication::setApplicationVersion(DEF2STR(APP_VERSION));
QCoreApplication::setOrganizationName("DynamX");
QCoreApplication::setOrganizationDomain("dynam.com");
QCommandLineParser parser;
parser.setApplicationDescription("CarPuter Daemon");
parser.addHelpOption();
parser.addVersionOption();
parser.process(a);
daemon carserver;
carserver;
a.exec();
}

View file

@ -0,0 +1,18 @@
#include "pluginbase.h"
PluginBase::PluginBase(QObject *parent) :
QObject(parent)
{
}
PluginBase::~PluginBase()
{
}
void PluginBase::startTimer()
{
connect(&this->m_updateTimer, &QTimer::timeout, this, &PluginBase::update);
this->m_updateTimer.setInterval(1000);
this->m_updateTimer.start();
}

52
client/CMakeLists.txt Normal file
View file

@ -0,0 +1,52 @@
cmake_minimum_required(VERSION 3.1.0)
project(sbcbmc-client VERSION 0.0.1)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
cmake_policy(SET CMP0071 NEW)
find_package(Qt5 COMPONENTS Core REQUIRED)
find_package(Qt5 COMPONENTS RemoteObjects REQUIRED)
find_package(PkgConfig REQUIRED)
#configure_file(include/config.h.in include/config.h)
set(HEADERS
)
set(SOURCES
source/main.cpp
)
qt5_generate_repc(REPSOURCES
${CMAKE_SOURCE_DIR}/plugins/dcdc-usb-200/source/dcdc_usb_200.rep
REPLICA
)
qt5_generate_repc(REPSOURCES
${CMAKE_SOURCE_DIR}/plugins/thermal/source/thermal.rep
REPLICA
)
add_executable(sbcbmc-client
${SOURCES}
${HEADERS}
${REPSOURCES}
)
target_include_directories(sbcbmc-client PRIVATE
"${PROJECT_BINARY_DIR}"
"${PROJECT_SOURCE_DIR}/include/"
"${PROJECT_SOURCE_DIR}/plugins/dcdc-usb-200/include/"
"${PROJECT_SOURCE_DIR}/plugins/thermal/include/"
${LibUSB_INCLUDE_DIRS}
)
target_link_libraries(sbcbmc-client Qt5::Core Qt5::RemoteObjects)

57
client/source/main.cpp Normal file
View file

@ -0,0 +1,57 @@
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QLoggingCategory>
#include <QtRemoteObjects/QRemoteObjectNode>
#include <QTimer>
Q_LOGGING_CATEGORY(ozwdaemon, "ozw.daemon");
#if 0
#define APP_VERSION #carputer_VERSION_MAJOR.#carputer_VERSION_MINOR.#carputer_VERSION_PATCH
#define DEF2STR2(x) #x
#define DEF2STR(x) DEF2STR2(x)
#endif
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
QCoreApplication::setApplicationName("carclient");
//QCoreApplication::setApplicationVersion(DEF2STR(APP_VERSION));
QCoreApplication::setOrganizationName("DynamX");
QCoreApplication::setOrganizationDomain("dynam.com");
QCommandLineParser parser;
parser.setApplicationDescription("CarClient");
parser.addHelpOption();
parser.addVersionOption();
parser.process(a);
QRemoteObjectNode node(QUrl("tcp://127.0.0.1:1999"));
QObject::connect(&node, &QRemoteObjectNode::remoteObjectAdded,
[](const QRemoteObjectSourceLocation& info){
qDebug() << "New source added : " << info;
});
qDebug() << "Waiting for registry ";
node.waitForRegistry(10000);
qDebug() << "Already here sources : " << node.registry()->sourceLocations();
QTimer timer;
timer.start(5000);
QObject::connect(&timer, &QTimer::timeout,
[&](){
qDebug() << "New sources list : " << node.registry()->sourceLocations();
});
QRemoteObjectDynamicReplica *plugin = node.acquireDynamic("plugins/thermal/thermal_zone1/x86_pkg_temp");
QRemoteObjectDynamicReplica *plugin2 = node.acquireDynamic("plugins/thermal/thermal_zone0/x86_pkg_temp");
a.exec();
}

7
plugins/CMakeLists.txt Normal file
View file

@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.1.0)
project(carputer)
add_subdirectory(dcdc-usb-200)
add_subdirectory(thermal)

View file

@ -0,0 +1,62 @@
cmake_minimum_required(VERSION 3.1.0)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
cmake_policy(SET CMP0071 NEW)
find_package(Qt5 COMPONENTS Core REQUIRED)
find_package(Qt5 COMPONENTS RemoteObjects REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LibUSB REQUIRED libusb)
set(HEADERS
include/dcdc-usb.h
include/dcdc-usb-200.h
)
set(SOURCES
source/dcdc-usb-comm.c
source/dcdc-usb-parse.c
source/dcdc-usb-proto.c
source/dcdc-usb-200.cpp
)
qt5_generate_repc(REPSOURCES
source/dcdc_usb_200.rep
SOURCE
)
#qt5_generate_repc(REPSOURCES
# source/dcdc_usb_200.rep
# REPLICA
# )
add_library(dcdc-usb-200
STATIC
${SOURCES}
${HEADERS}
${REPSOURCES}
)
target_include_directories(dcdc-usb-200 PRIVATE
"${PROJECT_BINARY_DIR}"
"${CMAKE_SOURCE_DIR}/car/include/"
${LibUSB_INCLUDE_DIRS}
)
target_include_directories(dcdc-usb-200 PUBLIC
"${PROJECT_SOURCE_DIR}/dcdc-usb-200/"
"${PROJECT_SOURCE_DIR}/dcdc-usb-200/include/"
"${CMAKE_CURRENT_BINARY_DIR}"
)
target_link_libraries(dcdc-usb-200 ${LibUSB_LIBRARIES})
target_link_libraries(dcdc-usb-200 Qt5::Core Qt5::RemoteObjects)

View file

@ -0,0 +1,36 @@
#ifndef DCDC_USB_200_H
#define DCDC_USB_200_H
#include <usb.h>
#include <QVariant>
#include "pluginbase.h"
#include "rep_dcdc_usb_200_source.h"
class DCDCUSB200_Properties : public DCDCUSB200_PropertiesSimpleSource {
Q_OBJECT
public:
DCDCUSB200_Properties(QObject *parent = nullptr);
~DCDCUSB200_Properties();
bool parsePacket(quint8 *data, int size);
};
class DCDCUSB200 : public PluginBase {
Q_OBJECT
public:
DCDCUSB200(QObject *parent = nullptr);
~DCDCUSB200();
bool initilize() override;
void update() override;
public Q_SLOTS:
void printUpdate(QVariant);
public Q_SLOTS:
bool registerSource(QRemoteObjectHostBase *host);
private:
struct usb_dev_handle *m_usb_handle;
DCDCUSB200_Properties m_properties;
};
#endif

View file

@ -0,0 +1,92 @@
#ifndef DCDCUSB_H
#define DCDCUSB_H
#define DCDC_VID 0x04d8
#define DCDC_PID 0xd003
#define MAX_TRANSFER_SIZE 24
/* USB communication wrappers */
struct usb_dev_handle * dcdc_connect();
int dcdc_send(struct usb_dev_handle *h, unsigned char *data, int size);
int dcdc_recv(struct usb_dev_handle *h, unsigned char *data, int size, int timeout);
int dcdc_setup(struct usb_dev_handle *h);
/* DCDC USB protocol */
int dcdc_get_status(struct usb_dev_handle *h, unsigned char *buf, int buflen);
int dcdc_set_vout(struct usb_dev_handle *h, double vout);
int dcdc_get_vout(struct usb_dev_handle *h, unsigned char *buf, int buflen);
int dcdc_parse_data(unsigned char *data, int size);
/* DCDC USB data parsing */
void dcdc_parse_values(unsigned char *data);
void dcdc_parse_cmd(unsigned char *data);
void dcdc_parse_internal_msg(unsigned char *data);
void dcdc_parse_mem(unsigned char *data);
/* from windows source with small sanity edits */
#define STATUS_OK 0x00
#define STATUS_ERASE 0x01
#define STATUS_WRITE 0x02
#define STATUS_READ 0x03
#define STATUS_ERROR 0xff
#define DCDCUSB_GET_ALL_VALUES 0x81
#define DCDCUSB_RECV_ALL_VALUES 0x82
#define DCDCUSB_CMD_OUT 0xB1
#define DCDCUSB_CMD_IN 0xB2
#define DCDCUSB_MEM_READ_OUT 0xA1
#define DCDCUSB_MEM_READ_IN 0xA2
#define DCDCUSB_MEM_WRITE_OUT 0xA3
#define DCDCUSB_MEM_WRITE_IN 0xA4
#define DCDCUSB_MEM_ERASE 0xA5
#define INTERNAL_MESG 0xFF
#define INTERNAL_MESG_DISCONNECTED 0x01
#define CMD_SET_AUX_WIN 0x01
#define CMD_SET_PW_SWITCH 0x02
#define CMD_SET_OUTPUT 0x03
#define CMD_WRITE_VOUT 0x06
#define CMD_READ_VOUT 0x07
#define CMD_INC_VOUT 0x0C
#define CMD_DEC_VOUT 0x0D
#define CMD_LOAD_DEFAULTS 0x0E
#define CMD_SCRIPT_START 0x10
#define CMD_SCRIPT_STOP 0x11
#define CMD_SLEEP 0x12
#define CMD_READ_REGULATOR_STEP 0x13
/* For reading out memory */
#define TYPE_CODE_MEMORY 0x00
#define TYPE_EPROM_EXTERNAL 0x01
#define TYPE_EPROM_INTERNAL 0x02
#define TYPE_CODE_SPLASH 0x03
#define FLASH_REPORT_ERASE_MEMORY 0xF2 /* AddressLo : AddressHi : AddressUp (anywhere inside the 64 byte-block to be erased) */
#define FLASH_REPORT_READ_MEMORY 0xF3 /* AddressLo : AddressHi : AddressUp : Data Length (1...32) */
#define FLASH_REPORT_WRITE_MEMORY 0xF4 /* AddressLo : AddressHi : AddressUp : Data Length (1...32) : Data.... */
#define KEYBD_REPORT_ERASE_MEMORY 0xB2 /* same as F2 but in keyboard mode */
#define KEYBD_REPORT_READ_MEMORY 0xB3 /* same as F3 but in keyboard mode */
#define KEYBD_REPORT_WRITE_MEMORY 0xB4 /* same as F4 but in keyboard mode */
#define KEYBD_REPORT_MEMORY 0x41 /* response to b3,b4 */
#define IN_REPORT_EXT_EE_DATA 0x31
#define OUT_REPORT_EXT_EE_READ 0xA1
#define OUT_REPORT_EXT_EE_WRITE 0xA2
#define IN_REPORT_INT_EE_DATA 0x32
#define OUT_REPORT_INT_EE_READ 0xA3
#define OUT_REPORT_INT_EE_WRITE 0xA4
/* MEASUREMENT CONSTANTS */
#define CT_RW (double)75
#define CT_R1 (double)49900
#define CT_R2 (double)1500
#define CT_RP (double)10000
#define CHECK_CHAR (unsigned char)0xAA /* used for line/write check */
#define MAX_MESSAGE_CNT 64
#endif /* DCDCUSB_H */

View file

@ -0,0 +1,125 @@
#include <QDebug>
#include <QMetaObject>
#include <QMetaMethod>
#include "dcdc-usb-200.h"
extern "C" {
#include "dcdc-usb.h"
}
DCDCUSB200::DCDCUSB200(QObject *parent) :
PluginBase(parent)
{
}
DCDCUSB200::~DCDCUSB200()
{
}
bool DCDCUSB200::initilize()
{
m_usb_handle = dcdc_connect();
if (m_usb_handle == NULL) {
qWarning() << "DCDCUSB200 - Can't Open USB Handle";
return false;
}
if (dcdc_setup(m_usb_handle) < 0) {
qWarning() << "DCDCUSB200 - Can't Setup Device";
return false;
}
connect(&this->m_properties, &DCDCUSB200_Properties::modeChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::timeConfigChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::voltageConfigChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::stateChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::vInChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::vIgnitionChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::vOutChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::powerSwitchChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::outputEnableChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::auxVInEnableChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::statusFlags1Changed, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::statusFlags2Changed, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::voltageFlagsChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::timerFlagsChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::flashPointerChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::timerWaitChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::timerVOutChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::timerVAuxChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::timerPwSwitchChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::timerOffDelayChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::timerHardOffDelayChanged, this, &DCDCUSB200::printUpdate);
connect(&this->m_properties, &DCDCUSB200_Properties::firmwareVersionChanged, this, &DCDCUSB200::printUpdate);
this->startTimer();
return true;
}
void DCDCUSB200::update()
{
unsigned char data[MAX_TRANSFER_SIZE];
int ret = dcdc_get_status(m_usb_handle, data, MAX_TRANSFER_SIZE);
if (ret <= 0) {
qWarning() << "DCDCUSB200 - Failed To Get Status from Device";
return;
}
this->m_properties.parsePacket(data, ret);
//dcdc_parse_data(data, ret);
qDebug() << "";
}
void DCDCUSB200::printUpdate(QVariant data) {
QObject *sender = QObject::sender();
int signalindex = QObject::senderSignalIndex();
const QMetaObject *mo = sender->metaObject();
qInfo() << "Changed Single: " << QString::fromLatin1(mo->method(signalindex).methodSignature()) << data;
}
bool DCDCUSB200::registerSource(QRemoteObjectHostBase *host) {
return host->enableRemoting(&this->m_properties, "plugins/DCDCUSB200");
}
DCDCUSB200_Properties::DCDCUSB200_Properties(QObject *parent) :
DCDCUSB200_PropertiesSimpleSource(parent)
{
}
DCDCUSB200_Properties::~DCDCUSB200_Properties()
{
}
bool DCDCUSB200_Properties::parsePacket(quint8 *data, int size) {
double value;
this->setMode((data[1] & 0x03));
this->setState(data[2]);
this->setVIn((double) data[3] * 0.1558f);
this->setVIgnition((double) data[4] * 0.1558f);
this->setVOut((float) data[5] * 0.1170f);
this->setStatusFlags1(data[6]);
this->setTimeConfig((data[1] >> 5) & 0x07);
this->setVoltageConfig((data[1] >> 2) & 0x07);
this->setPowerSwitch((data[6] & 0x04) ? true : false);
this->setOutputEnable((data[6] & 0x08) ? true : false);
this->setAuxVInEnable((data[6] & 0x10) ? true : false);
this->setStatusFlags2(data[7]);
this->setVoltageFlags(data[8]);
this->setTimerFlags(data[9]);
this->setFlashPointer(data[10]);
this->setTimerWait((data[11] << 8) | data[12]);
this->setTimerVOut((data[13] << 8) | data[14]);
this->setTimerVAux((data[15] << 8) | data[16]);
this->setTimerPwSwitch((data[17] << 8) | data[18]);
this->setTimerOffDelay((data[19] << 8) | data[20]);
this->setTimerHardOffDelay((data[21] << 8) | data[22]);
QString version("%1.%2");
this->setFirmwareVersion(version.arg(((data[23] >> 5) & 0x07)).arg((data[23] & 0x1F)));
return true;
}

View file

@ -0,0 +1,119 @@
/*
* Copyright (c) 2011 by Mini-Box.com, iTuner Networks Inc.
* Written by Nicu Pavel <npavel@mini-box.com>
* All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <usb.h>
#include "dcdc-usb.h"
#define P(t, v...) fprintf(stderr, t "\n", ##v)
int dcdc_send(struct usb_dev_handle *h, unsigned char *data, int size)
{
if (data == NULL)
return -1;
return usb_interrupt_write(h, USB_ENDPOINT_OUT + 1, (char *) data, size, 1000);
}
int dcdc_recv(struct usb_dev_handle *h, unsigned char *data, int size, int timeout)
{
if (data == NULL)
return -1;
return usb_interrupt_read(h, USB_ENDPOINT_IN + 1, (char *) data, size, timeout);
}
struct usb_dev_handle * dcdc_connect()
{
struct usb_bus *b;
struct usb_device *d;
struct usb_dev_handle *h = NULL;
usb_init();
usb_set_debug(0);
usb_find_busses();
usb_find_devices();
for (b = usb_get_busses(); b != NULL; b = b->next)
{
for (d = b->devices; d != NULL; d = d->next)
{
if ((d->descriptor.idVendor == DCDC_VID) &&
(d->descriptor.idProduct == DCDC_PID))
{
h = usb_open(d);
break;
}
}
}
return h;
}
int dcdc_setup(struct usb_dev_handle *h)
{
char buf[65535];
if (h == NULL)
return -1;
if (usb_get_driver_np(h, 0, buf, sizeof(buf)) == 0)
{
if (usb_detach_kernel_driver_np(h, 0) < 0)
{
fprintf(stderr, "Cannot detach from kernel driver\n");
return -2;
}
}
if (usb_set_configuration(h, 1) < 0)
{
fprintf(stderr, "Cannot set configuration 1 for the device\n");
return -3;
}
usleep(1000);
if (usb_claim_interface(h, 0) < 0)
{
fprintf(stderr, "Cannot claim interface 0\n");
return -4;
}
if (usb_set_altinterface(h, 0) < 0)
{
fprintf(stderr, "Cannot set alternate configuration\n");
return -5;
}
if (usb_control_msg(h, USB_TYPE_CLASS + USB_RECIP_INTERFACE,
0x000000a, 0x0000000, 0x0000000, buf, 0x0000000, 1000)
< 0)
{
fprintf(stderr, "Cannot send control message\n");
return -6;
}
return 0;
}

View file

@ -0,0 +1,131 @@
/*
* Copyright (c) 2011 by Mini-Box.com, iTuner Networks Inc.
* Written by Nicu Pavel <npavel@mini-box.com>
* All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include "dcdc-usb.h"
#define P(t, v...) fprintf(stderr, t "\n", ##v)
static int bytes2int(unsigned char c1, unsigned char c2)
{
int i = c1;
i = i << 8;
i = i | c2;
return i;
}
static int byte2bits(unsigned char c)
{
int i,n = 0;
for (i = 0; i < 8; i++)
{
n = n * 10;
if ((c >> i) & 1)
n = n + 1;
}
return n;
}
static double byte2vout(unsigned char c)
{
double rpot = ((double) c) * CT_RP / (double)257 + CT_RW;
double voltage = (double)80 * ((double) 1 + CT_R1/(rpot + CT_R2));
voltage = floor(voltage);
return voltage/100;
}
void dcdc_parse_values(unsigned char *data)
{
int mode, state, status;
float ignition_voltage, input_voltage, output_voltage;
mode = data[1];
state = data[2];
input_voltage = (float) data[3] * 0.1558f;
ignition_voltage = (float) data[4] * 0.1558f;
output_voltage = (float) data[5] * 0.1170f;
status = data[6];
switch(mode & 0x03)
{
case 0: P("mode: 0 (dumb)"); break;
case 1: P("mode: 1 (automotive)"); break;
case 2: P("mode: 2 (script)"); break;
case 3: P("mode: 3 (ups)");break;
}
P("time config: %d", (mode >> 5) & 0x07);
P("voltage config: %d", (mode >> 2) & 0x07);
P("state: %d", state);
P("input voltage: %.2f", input_voltage);
P("ignition voltage: %.2f", ignition_voltage);
P("output voltage: %2f", output_voltage);
P("power switch: %s", ((status & 0x04) ? "On":"Off"));
P("output enable: %s", ((status & 0x08) ? "On":"Off"));
P("aux vin enable %s", ((status & 0x10) ? "On":"Off"));
P("status flags 1: %d", byte2bits(data[6]));
P("status flags 2: %d", byte2bits(data[7]));
P("voltage flags: %d", byte2bits(data[8]));
P("timer flags: %d", byte2bits(data[9]));
P("flash pointer: %d", data[10]);
P("timer wait: %d", bytes2int(data[11], data[12]));
P("timer vout: %d", bytes2int(data[13], data[14]));
P("timer vaux: %d", bytes2int(data[15], data[16]));
P("timer pw switch: %d", bytes2int(data[17], data[18]));
P("timer off delay: %d", bytes2int(data[19], data[20]));
P("timer hard off: %d", bytes2int(data[21], data[22]));
P("version: %d.%d", ((data[23] >> 5) & 0x07), (data[23] & 0x1F));
}
void dcdc_parse_cmd(unsigned char *data)
{
if (data[1] == 0)
{
if (data[2] == CMD_READ_REGULATOR_STEP)
{
P("regulator step: %d", data[3]);
}
else
{
P("output voltage: %.2f", byte2vout(data[3]));
}
}
else
{
if (data[2] == CMD_READ_REGULATOR_STEP)
{
P("regulator step not defined");
}
}
}
void dcdc_parse_internal_msg(unsigned char *data)
{
P("Parsing INTERNAL MESSAGE: Not implemented");
}
void dcdc_parse_mem(unsigned char *data)
{
P("Parsing MEM READ IN: Not implemented");
}

View file

@ -0,0 +1,130 @@
/*
* Copyright (c) 2011 by Mini-Box.com, iTuner Networks Inc.
* Written by Nicu Pavel <npavel@mini-box.com>
* All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <usb.h>
#include "dcdc-usb.h"
/* transforms user value to a value understood by device */
static int vout2dev(double vout)
{
double rpot = (double)0.8 * CT_R1 / (vout - (double)0.8) - CT_R2;
double result = (257 * (rpot-CT_RW) / CT_RP);
if (result<0) result = 0;
if (result>255) result = 255;
return (unsigned char)result;
}
static int dcdc_send_command(struct usb_dev_handle *h, unsigned char cmd, unsigned char val)
{
unsigned char c[3];
c[0] = DCDCUSB_CMD_OUT;
c[1] = cmd;
c[2] = val;
return dcdc_send(h, c, 3);
}
int dcdc_get_status(struct usb_dev_handle *h, unsigned char *buf, int buflen)
{
unsigned char c[2];
int ret = 0;
if (buflen < MAX_TRANSFER_SIZE)
return -1;
c[0] = DCDCUSB_GET_ALL_VALUES;
c[1] = 0;
if (dcdc_send(h, c, 2) < 0)
{
fprintf(stderr, "Cannot send command to device\n");
return -2;
}
if ((ret = dcdc_recv(h, buf, MAX_TRANSFER_SIZE, 1000)) < 0)
{
fprintf(stderr, "Cannot get device status\n");
return -3;
}
return ret;
}
int dcdc_set_vout(struct usb_dev_handle *h, double vout)
{
if (vout < 5) vout = 5;
if (vout > 24) vout = 24;
return dcdc_send_command(h, CMD_WRITE_VOUT, vout2dev(vout));
}
/* value will be shown on dcdc_parse_data() */
int dcdc_get_vout(struct usb_dev_handle *h, unsigned char *buf, int buflen)
{
int ret = 0;
if (buflen < MAX_TRANSFER_SIZE)
return -1;
if ((ret = dcdc_send_command(h, CMD_READ_VOUT, 0)) < 0)
return ret;
ret = dcdc_recv(h, buf, MAX_TRANSFER_SIZE, 1000);
return ret;
}
int dcdc_parse_data(unsigned char *data, int size)
{
if (data == NULL)
return -1;
#ifdef DEBUG
int i;
for (i = 0; i < size; i++)
{
if (i % 8 == 0) fprintf(stderr, "\n");
fprintf(stderr, "[%02d] = 0x%02x ", i, data[i]);
}
fprintf(stderr, "\n");
#endif
switch(data[0])
{
case DCDCUSB_RECV_ALL_VALUES: dcdc_parse_values(data);break;
case DCDCUSB_CMD_IN: dcdc_parse_cmd(data); break;
case INTERNAL_MESG: dcdc_parse_internal_msg(data); break;
case DCDCUSB_MEM_READ_IN: dcdc_parse_mem(data); break;
default:
fprintf(stderr, "Unknown message\n");
}
return 0;
}

View file

@ -0,0 +1,25 @@
class DCDCUSB200_Properties {
PROP(QVariant mode SOURCEONLYSETTER)
PROP(QVariant timeConfig SOURCEONLYSETTER)
PROP(QVariant voltageConfig SOURCEONLYSETTER)
PROP(QVariant state SOURCEONLYSETTER)
PROP(QVariant vIn SOURCEONLYSETTER)
PROP(QVariant vIgnition SOURCEONLYSETTER)
PROP(QVariant vOut SOURCEONLYSETTER)
PROP(QVariant powerSwitch SOURCEONLYSETTER)
PROP(QVariant outputEnable SOURCEONLYSETTER)
PROP(QVariant auxVInEnable SOURCEONLYSETTER)
PROP(QVariant statusFlags1 SOURCEONLYSETTER)
PROP(QVariant statusFlags2 SOURCEONLYSETTER)
PROP(QVariant voltageFlags SOURCEONLYSETTER)
PROP(QVariant timerFlags SOURCEONLYSETTER)
PROP(QVariant flashPointer SOURCEONLYSETTER)
PROP(QVariant timerWait SOURCEONLYSETTER)
PROP(QVariant timerVOut SOURCEONLYSETTER)
PROP(QVariant timerVAux SOURCEONLYSETTER)
PROP(QVariant timerPwSwitch SOURCEONLYSETTER)
PROP(QVariant timerOffDelay SOURCEONLYSETTER)
PROP(QVariant timerHardOffDelay SOURCEONLYSETTER)
PROP(QVariant firmwareVersion SOURCEONLYSETTER)
}

View file

@ -0,0 +1,51 @@
cmake_minimum_required(VERSION 3.1.0)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
cmake_policy(SET CMP0071 NEW)
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
cmake_policy(SET CMP0071 NEW)
find_package(Qt5 COMPONENTS Core REQUIRED)
find_package(Qt5 COMPONENTS RemoteObjects REQUIRED)
set(HEADERS
include/thermal.h
)
set(SOURCES
source/thermal.cpp
)
qt5_generate_repc(REPSOURCES
source/thermal.rep
SOURCE
)
add_library(thermal
STATIC
${SOURCES}
${HEADERS}
${REPSOURCES}
)
target_include_directories(thermal PRIVATE
"${PROJECT_BINARY_DIR}"
"${CMAKE_SOURCE_DIR}/car/include/"
)
target_include_directories(thermal PUBLIC
"${PROJECT_SOURCE_DIR}/thermal/"
"${PROJECT_SOURCE_DIR}/thermal/include/"
"${CMAKE_CURRENT_BINARY_DIR}"
)
target_link_libraries(thermal Qt5::Core Qt5::RemoteObjects)

View file

@ -0,0 +1,39 @@
#ifndef THERMAL_H
#define THERMAL_H
#include <QDir>
#include "pluginbase.h"
#include "rep_thermal_source.h"
class Thermal_Properties : public Thermal_PropertiesSimpleSource {
Q_OBJECT
public:
Thermal_Properties(QString name, QObject *parent = nullptr);
~Thermal_Properties();
Q_PROPERTY(QDir directory READ getDirectory WRITE setDirectory);
QDir getDirectory() { return this->m_directory; }
void processPacket(QVariant);
public Q_SLOTS:
void setDirectory(QDir directory);
private:
QDir m_directory;
};
class Thermal : public PluginBase {
Q_OBJECT
public:
Thermal(QObject *parent = nullptr);
~Thermal();
bool initilize() override;
void update() override;
public Q_SLOTS:
void printUpdate(QVariant);
bool registerSource(QRemoteObjectHostBase *host) override;
private:
QHash<QString, Thermal_Properties*> m_properties;
};
#endif

View file

@ -0,0 +1,101 @@
#include <QDebug>
#include <QMetaObject>
#include <QMetaMethod>
#include <QDir>
#include "thermal.h"
Thermal::Thermal(QObject *parent) :
PluginBase(parent)
{
}
Thermal::~Thermal()
{
}
bool Thermal::initilize()
{
QDir sysclass("/sys/class/thermal/");
QStringList thermals = sysclass.entryList(QStringList() << "thermal_zone*", QDir::Dirs);
foreach(QString directory, thermals) {
/* Get the name for this Thermal class */
QFile thermal("/sys/class/thermal/"+directory+"/type");
if (!thermal.exists())
{
qWarning() << "Thermal Type Entry does not Exist for " << directory;
continue;
}
if (!thermal.open(QIODevice::ReadOnly | QIODevice::Text))
{
qWarning() << "Can Not Open Thermal Type Entry for " << directory;
continue;
}
QString name = thermal.readLine();
Thermal_Properties *tp = new Thermal_Properties(directory+"/"+name.simplified(), this);
tp->setDirectory(QDir("/sys/class/thermal/"+directory));
this->m_properties.insert(directory+"/"+name.simplified(), tp);
connect(tp, &Thermal_Properties::tempChanged, this, &Thermal::printUpdate);
}
this->startTimer();
return true;
}
void Thermal::update()
{
foreach(Thermal_Properties *tp, this->m_properties) {
QFile temp(tp->getDirectory().absoluteFilePath("temp"));
if (!temp.exists()) {
qWarning() << "Temp Entry for " << tp->name() << " doesn't exist";
continue;
}
if (!temp.open(QIODevice::ReadOnly | QIODevice::Text))
{
qWarning() << "Can't Open Temp File for " << tp->name();
continue;
}
QString value = temp.readLine().simplified();
tp->processPacket(value);
}
}
void Thermal::printUpdate(QVariant data) {
QObject *sender = QObject::sender();
int signalindex = QObject::senderSignalIndex();
const QMetaObject *mo = sender->metaObject();
qInfo() << "Changed Single: " << sender->objectName() << QString::fromLatin1(mo->method(signalindex).methodSignature()) << data.toString();
}
bool Thermal::registerSource(QRemoteObjectHostBase *host) {
bool ret = false;
foreach(Thermal_Properties *tp, this->m_properties) {
ret |= host->enableRemoting(tp, "plugins/thermal/"+tp->name().toString());
}
return ret;
}
Thermal_Properties::Thermal_Properties(QString name, QObject *parent) :
Thermal_PropertiesSimpleSource(parent)
{
this->setObjectName(name);
this->setName(name);
qDebug() << "New Thermal Property Found: " << name;
}
Thermal_Properties::~Thermal_Properties()
{
}
void Thermal_Properties::setDirectory(QDir directory)
{
this->m_directory = directory;
}
void Thermal_Properties::processPacket(QVariant data)
{
this->setTemp(data.toInt() * 0.001);
}

View file

@ -0,0 +1,4 @@
class Thermal_Properties {
PROP(QVariant name SOURCEONLYSETTER)
PROP(QVariant temp SOURCEONLYSETTER)
}