Commit 69d2e3fc authored by Roland Singer's avatar Roland Singer

* fixed bug which caused bug

* adjustment and minor redesign of library syntax
* improvement of mhwd script
parent b2263c4e
cmake_minimum_required (VERSION 2.6)
project(mhwd)
SET(CMAKE_INSTALL_PREFIX "/usr/")
add_definitions(-Wall)
add_subdirectory(libmhwd)
add_subdirectory(mhwd)
add_subdirectory(scripts)
Implement config dependency installing and conflict checking
allow to set the base lib dir
add architecture support and install dependencies for it...
......@@ -21,3 +21,8 @@ set( LIB_LIBS hd)
add_library (mhwd SHARED ${LIB_SOURCES} ${LIB_HEADERS})
target_link_libraries(mhwd ${LIB_LIBS})
INSTALL(TARGETS mhwd
LIBRARY DESTINATION lib
)
......@@ -20,9 +20,11 @@
#define MHWD_CONFIG_NAME "MHWDCONFIG"
#define MHWD_USB_CONFIG_DIR "/var/lib/mhwd/usb"
#define MHWD_PCI_CONFIG_DIR "/var/lib/mhwd/pci"
#define MHWD_USB_CONFIG_DIR "/var/lib/mhwd/db/usb"
#define MHWD_PCI_CONFIG_DIR "/var/lib/mhwd/db/pci"
#define MHWD_USB_DATABASE_DIR "/var/lib/mhwd/local/usb"
#define MHWD_PCI_DATABASE_DIR "/var/lib/mhwd/local/pci"
#define MHWD_CACHE_DIR "/var/cache/pacman/pkg"
#define MHWD_SCRIPT_PATH "/var/lib/mhwd/script/mhwd"
#define MHWD_SCRIPT_PATH "/var/lib/mhwd/scripts/mhwd"
#define MHWD_PM_CACHE_DIR "/var/cache/pacman/pkg"
#define MHWD_PM_CONFIG "/etc/pacman.conf"
This diff is collapsed.
......@@ -53,18 +53,19 @@ namespace mhwd {
struct Device {
TYPE type;
std::string className, deviceName, vendorName, classID, deviceID, vendorID;
std::vector<mhwd::Config> availableConfigs, installedConfigs;
std::vector<mhwd::Config*> availableConfigs, installedConfigs;
};
struct Data {
struct Environment {
std::string cachePath;
std::string PMCachePath, PMConfigPath;
bool syncPackageManagerDatabase;
void (*messageFunc)(std::string);
};
std::string lastError;
std::vector<mhwd::Device> USBDevices, PCIDevices;
std::vector<mhwd::Config> installedUSBConfigs, installedPCIConfigs, invalidConfigs;
std::vector<mhwd::Device*> USBDevices, PCIDevices;
std::vector<mhwd::Config*> installedUSBConfigs, installedPCIConfigs, allUSBConfigs, allPCIConfigs, invalidConfigs;
Environment environment;
};
......@@ -74,28 +75,25 @@ namespace mhwd {
//#################//
void initData(mhwd::Data *data);
void fillData(mhwd::Data *data);
void freeData(mhwd::Data *data);
void updateConfigData(mhwd::Data *data);
void updateInstalledConfigData(mhwd::Data *data);
void printDeviceDetails(mhwd::TYPE type, FILE *f = stdout);
bool installConfig(mhwd::Data *data, mhwd::Config *config);
bool uninstallConfig(mhwd::Data *data, mhwd::Config *config);
mhwd::Config* getInstalledConfig(mhwd::Data *data, const std::string configName, const TYPE configType);
mhwd::Config* getDatabaseConfig(mhwd::Data *data, const std::string configName, const TYPE configType);
mhwd::Config* getAvailableConfig(mhwd::Data *data, const std::string configName, const TYPE configType);
bool installConfig(mhwd::Data *data, mhwd::Config *config);
bool uninstallConfig(mhwd::Data *data, mhwd::Config *config);
}
#else
namespace mhwd {
extern void initData(mhwd::Data *data);
extern void fillData(mhwd::Data *data);
extern void printDeviceDetails(mhwd::TYPE type, FILE *f = stdout);
extern bool installConfig(mhwd::Data *data, mhwd::Config *config);
extern bool uninstallConfig(mhwd::Data *data, mhwd::Config *config);
extern mhwd::Config* getInstalledConfig(mhwd::Data *data, const std::string configName, const TYPE configType);
extern mhwd::Config* getAvailableConfig(mhwd::Data *data, const std::string configName, const TYPE configType);
// TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
......
......@@ -25,6 +25,7 @@
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <sys/stat.h>
#include <sys/types.h>
......@@ -41,10 +42,9 @@ namespace mhwd {
//###############//
//### Devices ###//
//###############//
void setDevices(mhwd::Data *data, mhwd::TYPE type);
void fillDevices(mhwd::Data *data, mhwd::TYPE type);
Vita::string from_Hex(uint16_t hexnum, int fill);
Vita::string from_CharArray(char* c);
void addConfigSorted(std::vector<mhwd::Config>* configs, mhwd::Config* config);
//###############//
......@@ -52,16 +52,16 @@ namespace mhwd {
//###############//
enum SCRIPTOPERATION { SCRIPTOPERATION_INSTALL, SCRIPTOPERATION_REMOVE };
bool checkDependenciesConflicts(mhwd::Data *data, mhwd::Config *config);
void updateInstalledConfigData(mhwd::Data *data);
void setInstalledConfigs(mhwd::Data *data, mhwd::TYPE type);
void setMatchingConfigs(mhwd::Data *data, std::vector<mhwd::Device>* devices, mhwd::TYPE type, bool setAsInstalled = false);
void setMatchingConfigs(std::vector<mhwd::Device>* devices, std::vector<mhwd::Config>* configs, bool setAsInstalled = false);
void setMatchingConfig(mhwd::Config* config, std::vector<mhwd::Device>* devices, bool setAsInstalled = false);
void fillInstalledConfigs(mhwd::Data *data, mhwd::TYPE type);
void fillAllConfigs(mhwd::Data *data, mhwd::TYPE type);
void setMatchingConfigs(std::vector<mhwd::Device*>* devices, std::vector<mhwd::Config*>* configs, bool setAsInstalled);
void setMatchingConfig(mhwd::Config* config, std::vector<mhwd::Device*>* devices, bool setAsInstalled);
void addConfigSorted(std::vector<mhwd::Config*>* configs, mhwd::Config* config);
bool fillConfig(mhwd::Config *config, std::string configPath, mhwd::TYPE type);
bool readConfigFile(mhwd::Config *config, std::string configPath);
std::vector<std::string> splitValue(Vita::string str, Vita::string onlyEnding = "");
Vita::string getRightConfigPath(Vita::string str, Vita::string baseConfigPath);
bool checkDependenciesConflicts(mhwd::Data *data, mhwd::Config *config);
//#####################################//
......
......@@ -17,3 +17,8 @@ set( LIBS mhwd)
add_executable(mhwd-bin ${SOURCES} ${HEADERS})
target_link_libraries(mhwd-bin ${LIBS})
set_target_properties(mhwd-bin PROPERTIES OUTPUT_NAME mhwd)
INSTALL(TARGETS mhwd-bin
RUNTIME DESTINATION bin
)
......@@ -19,6 +19,7 @@
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <iomanip>
#include <iostream>
......@@ -30,6 +31,10 @@
using namespace std;
#define CONSOLE_COLOR_RESET "\e[m"
#define CONSOLE_COLOR "\e[1m\e[31m"
enum ARGUMENTS {
ARG_NONE = 0x0000,
ARG_SHOWAVAILABLE = 0x0001,
......@@ -48,16 +53,19 @@ mhwd::Data data;
vector<string> configList;
mhwd::TYPE operationType;
bool checkRoot();
void messageFunc(string str);
void printArrow();
void printHelp();
void printAvailableConfigs(mhwd::TYPE type);
void printInstalledConfigs(mhwd::TYPE type);
void checkInvalidConfigs();
bool installConfigs();
bool removeConfigs();
int main (int argc, char *argv[])
{
// Set up data object
......@@ -124,17 +132,38 @@ int main (int argc, char *argv[])
arguments = (ARGUMENTS)(arguments | ARG_REMOVE);
}
else if (strcmp(argv[nArg], "--cachedir") == 0) {
else if (strcmp(argv[nArg], "--pmcachedir") == 0) {
if (nArg + 1 >= argc) {
cout << "invalid use of option: --pmcachedir" << endl << endl;
printHelp();
return 1;
}
data.environment.PMCachePath = Vita::string(argv[++nArg]).trim("\"").trim();
}
else if (strcmp(argv[nArg], "--pmconfig") == 0) {
if (nArg + 1 >= argc) {
cout << "invalid use of option: --cachedir" << endl << endl;
cout << "invalid use of option: --pmconfig" << endl << endl;
printHelp();
return 1;
}
data.environment.cachePath = Vita::string(argv[++nArg]).trim("\"").trim();
data.environment.PMConfigPath = Vita::string(argv[++nArg]).trim("\"").trim();
}
else if ((arguments & ARG_INSTALL) || (arguments & ARG_REMOVE)) {
configList.push_back(Vita::string(argv[nArg]).toLower());
bool found = false;
std::string name = Vita::string(argv[nArg]).toLower();
// Check if already in list
for (vector<string>::iterator iter = configList.begin(); iter != configList.end(); iter++) {
if ((*iter) == name) {
found = true;
break;
}
}
if (!found)
configList.push_back(name);
}
else {
cout << "invalid option: " << argv[nArg] << endl << endl;
......@@ -144,8 +173,9 @@ int main (int argc, char *argv[])
}
// Check if arguments are right
if ((arguments & ARG_INSTALL) && (arguments & ARG_REMOVE)) {
cout << "Error: install and remove option can be used only seperate!" << endl << endl;
cout << "install and remove option can be used only seperate!" << endl << endl;
printHelp();
return 1;
}
......@@ -159,11 +189,15 @@ int main (int argc, char *argv[])
arguments = (ARGUMENTS)(arguments | ARG_SHOWUSB | ARG_SHOWPCI);
// Fill data with hardware informations
mhwd::fillData(&data);
// Check for invalid configs
checkInvalidConfigs();
for (vector<mhwd::Config*>::iterator iterator = data.invalidConfigs.begin(); iterator != data.invalidConfigs.end(); iterator++) {
cout << "Warning: Config '" << (*iterator)->configPath << "' is invalid!" << endl;
}
// Check set arguments
if (arguments & ARG_SHOWDETAILED && arguments & ARG_SHOWPCI)
......@@ -184,26 +218,46 @@ int main (int argc, char *argv[])
if (arguments & ARG_INSTALL) {
if (!installConfigs())
if (!checkRoot() || !installConfigs())
return 1;
}
else if (arguments & ARG_REMOVE) {
if (!removeConfigs())
if (!checkRoot() || !removeConfigs())
return 1;
}
// Cleanup
mhwd::freeData(&data);
return 0;
}
bool checkRoot() {
if (getuid()) {
cout << "you cannot perform this operation unless you are root!" << endl;
return false;
}
return true;
}
void messageFunc(string str) {
cout << str;
}
void printArrow() {
cout << CONSOLE_COLOR << "> " << CONSOLE_COLOR_RESET;
}
void printHelp() {
cout << "Usage: mhwd [OPTIONS] <config(s)>" << endl << endl;
cout << " --pci\t\t\t\t\tshow only pci devices and driver configs" << endl;
......@@ -215,7 +269,8 @@ void printHelp() {
cout << " -sh/--showhardware\t\t\tshow detailed hardware info" << endl;
cout << " -i/--install <usb/pci> <config(s)>\tinstall driver config(s)" << endl;
cout << " -r/--remove <usb/pci> <config(s)>\tremove driver config(s)" << endl;
cout << " --cachedir <path>\t\t\tset package manager cache path" << endl;
cout << " --pmcachedir <path>\t\t\tset package manager cache path" << endl;
cout << " --pmconfig <path>\t\t\tset package manager config" << endl;
cout << endl;
}
......@@ -223,7 +278,7 @@ void printHelp() {
void printAvailableConfigs(mhwd::TYPE type) {
string beg;
vector<mhwd::Device> *devices;
vector<mhwd::Device*> *devices;
if (type == mhwd::TYPE_USB) {
beg = "USB";
......@@ -234,42 +289,46 @@ void printAvailableConfigs(mhwd::TYPE type) {
devices = &data.PCIDevices;
}
bool found = false;
for (vector<mhwd::Device>::iterator dev_iter = devices->begin(); dev_iter != devices->end(); dev_iter++) {
if ((*dev_iter).availableConfigs.empty() && (*dev_iter).installedConfigs.empty())
for (vector<mhwd::Device*>::iterator dev_iter = devices->begin(); dev_iter != devices->end(); dev_iter++) {
if ((*dev_iter)->availableConfigs.empty() && (*dev_iter)->installedConfigs.empty())
continue;
found = true;
cout << endl << setfill('-') << setw(80) << "-" << setfill(' ') << endl;
cout << "> " << beg << " Device: " << (*dev_iter).className << " (" << (*dev_iter).classID << ") ";
cout << (*dev_iter).vendorName << " (" << (*dev_iter).vendorID << ") ";
cout << (*dev_iter).deviceName << " (" << (*dev_iter).deviceID << ") " << endl;
cout << "> " << beg << " Device: " << (*dev_iter)->className << " (" << (*dev_iter)->classID << ") ";
cout << (*dev_iter)->vendorName << " (" << (*dev_iter)->vendorID << ") ";
cout << (*dev_iter)->deviceName << " (" << (*dev_iter)->deviceID << ") " << endl;
cout << setfill('-') << setw(80) << "-" << setfill(' ') << endl;
if (!(*dev_iter).installedConfigs.empty())
if (!(*dev_iter)->installedConfigs.empty())
cout << " > INSTALLED:" << endl;
for (vector<mhwd::Config>::iterator iterator = (*dev_iter).installedConfigs.begin(); iterator != (*dev_iter).installedConfigs.end(); iterator++) {
cout << endl << " NAME:\t" << (*iterator).name << endl;
cout << " VERSION:\t" << (*iterator).version << endl;
cout << " INFO:\t" << (*iterator).info << endl;
cout << " PRIORITY:\t" << (*iterator).priority << endl;
if ((*iterator).freedriver)
for (vector<mhwd::Config*>::iterator iterator = (*dev_iter)->installedConfigs.begin(); iterator != (*dev_iter)->installedConfigs.end(); iterator++) {
cout << endl << " NAME:\t" << (*iterator)->name << endl;
cout << " VERSION:\t" << (*iterator)->version << endl;
cout << " INFO:\t" << (*iterator)->info << endl;
cout << " PRIORITY:\t" << (*iterator)->priority << endl;
if ((*iterator)->freedriver)
cout << " FREEDRIVER:\ttrue" << endl;
else
cout << " FREEDRIVER:\tfalse" << endl;
}
if (!(*dev_iter).installedConfigs.empty())
if (!(*dev_iter)->installedConfigs.empty())
cout << endl << endl;
if (!(*dev_iter).availableConfigs.empty())
if (!(*dev_iter)->availableConfigs.empty())
cout << " > AVAILABLE:" << endl;
for (vector<mhwd::Config>::iterator iterator = (*dev_iter).availableConfigs.begin(); iterator != (*dev_iter).availableConfigs.end(); iterator++) {
cout << endl << " NAME:\t" << (*iterator).name << endl;
cout << " VERSION:\t" << (*iterator).version << endl;
cout << " INFO:\t" << (*iterator).info << endl;
cout << " PRIORITY:\t" << (*iterator).priority << endl;
if ((*iterator).freedriver)
for (vector<mhwd::Config*>::iterator iterator = (*dev_iter)->availableConfigs.begin(); iterator != (*dev_iter)->availableConfigs.end(); iterator++) {
cout << endl << " NAME:\t" << (*iterator)->name << endl;
cout << " VERSION:\t" << (*iterator)->version << endl;
cout << " INFO:\t" << (*iterator)->info << endl;
cout << " PRIORITY:\t" << (*iterator)->priority << endl;
if ((*iterator)->freedriver)
cout << " FREEDRIVER:\ttrue" << endl;
else
cout << " FREEDRIVER:\tfalse" << endl;
......@@ -277,13 +336,18 @@ void printAvailableConfigs(mhwd::TYPE type) {
cout << endl;
}
if (!found) {
cout << "> No configs for " << beg << " devices found!" << endl;
return;
}
}
void printInstalledConfigs(mhwd::TYPE type) {
string beg;
vector<mhwd::Config> *configs;
vector<mhwd::Config*> *configs;
if (type == mhwd::TYPE_USB) {
beg = "USB";
......@@ -294,16 +358,17 @@ void printInstalledConfigs(mhwd::TYPE type) {
configs = &data.installedPCIConfigs;
}
if (configs->empty())
if (configs->empty()) {
cout << "> No installed configs for " << beg << " devices found!" << endl;
return;
for (vector<mhwd::Config>::iterator iterator = configs->begin(); iterator != configs->end(); iterator++) {
cout << endl << " NAME:\t" << (*iterator).name << endl;
}
for (vector<mhwd::Config*>::iterator iterator = configs->begin(); iterator != configs->end(); iterator++) {
cout << endl << " NAME:\t" << (*iterator)->name << endl;
cout << " ATTACHED:\t" << beg << endl;
cout << " VERSION:\t" << (*iterator).version << endl;
cout << " INFO:\t" << (*iterator).info << endl;
cout << " PRIORITY:\t" << (*iterator).priority << endl;
if ((*iterator).freedriver)
cout << " VERSION:\t" << (*iterator)->version << endl;
cout << " INFO:\t" << (*iterator)->info << endl;
cout << " PRIORITY:\t" << (*iterator)->priority << endl;
if ((*iterator)->freedriver)
cout << " FREEDRIVER:\ttrue" << endl;
else
cout << " FREEDRIVER:\tfalse" << endl;
......@@ -314,57 +379,7 @@ void printInstalledConfigs(mhwd::TYPE type) {
void checkInvalidConfigs() {
for (vector<mhwd::Config>::iterator iterator = data.invalidConfigs.begin(); iterator != data.invalidConfigs.end(); iterator++) {
cout << "Warning: Config '" << (*iterator).configPath << "' is invalid!" << endl;
}
}
/* TODO: Implement config dependency installing and conflict checking
bool installConfigs(vector<string> &configList, vector<string> &skipList) {
for (vector<string>::iterator iter = configList.begin(); iter != configList.end(); iter++) {
bool skip = false;
for (vector<string>::iterator skip_iter = skipList.begin(); skip_iter != skipList.end(); skip_iter++) {
if ((*iter) == (*skip_iter)) {
skip = true;
break;
}
}
if (skip)
continue;
skipList.push_back((*iter));
mhwd::Config *config = mhwd::getAvailableConfig(&data, (*iter), operationType);
if (config == NULL) {
cout << "Config " << (*iter) << " does not exist!" << endl;
return false;
}
else if (!config->dependencies.empty()) {
cout << "> Installing dependencies of " << config->name << "..." << endl;
if (!installConfigs(config->dependencies, skipList))
return false;
}
cout << "> Installing Config " << config->name << "..." << endl;
if (!mhwd::installConfig(&data, config)) {
cout << "Installation failed: " << data.lastError << endl;
return false;
}
}
return true;
}*/
// TODO: Implement config dependency installing and conflict checking
bool installConfigs() {
for (vector<string>::iterator iter = configList.begin(); iter != configList.end(); iter++) {
......@@ -380,20 +395,34 @@ bool installConfigs() {
return false;
}
else if (installedConfig != NULL && arguments & ARG_FORCE) {
cout << "> Removing Config " << installedConfig->name << "..." << endl;
printArrow();
cout << "Removing Config " << installedConfig->name << "..." << endl;
if (!mhwd::uninstallConfig(&data, installedConfig)) {
cout << "Remove failed: " << data.lastError << endl;
return false;
}
printArrow();
cout << "Removed Config " << installedConfig->name << endl;
// Update mhwd data object
mhwd::updateInstalledConfigData(&data);
}
cout << "> Installing Config " << config->name << "..." << endl;
printArrow();
cout << "Installing Config " << config->name << "..." << endl;
if (!mhwd::installConfig(&data, config)) {
cout << "Installation failed: " << data.lastError << endl;
return false;
}
printArrow();
cout << "Installed Config " << config->name << endl;
// Update mhwd data object
mhwd::updateInstalledConfigData(&data);
}
return true;
......@@ -401,7 +430,7 @@ bool installConfigs() {
bool removeConfigs() {
bool removeConfigs() {
for (vector<string>::iterator iter = configList.begin(); iter != configList.end(); iter++) {
mhwd::Config *config = mhwd::getInstalledConfig(&data, (*iter), operationType);
......@@ -410,12 +439,19 @@ bool removeConfigs() {
return false;
}
cout << "> Removing Config " << config->name << "..." << endl;
printArrow();
cout << "Removing Config " << config->name << "..." << endl;
if (!mhwd::uninstallConfig(&data, config)) {
cout << "Remove failed: " << data.lastError << endl;
return false;
}
printArrow();
cout << "Removed Config " << config->name << endl;
// Update mhwd data object
mhwd::updateInstalledConfigData(&data);
}
return true;
......
#!/bin/bash
### Module Functions ###
mhwd_load_Module()
{
local loadedmodules="$(ls /sys/module)"
local moduleisloaded="false"
if [ -z "${loadedmodules}" ]; then
return 1 #false
fi
for loadedmodule in ${loadedmodules} ; do
if [ "$1" == "$loadedmodule" ]; then
moduleisloaded="true"
fi
done
if [ "$moduleisloaded" == "false" ]; then
modprobe $1
fi
return 0 #true
}
mhwd_unload_Module()
{
local loadedmodules="$(ls /sys/module)"
if [ -z "${loadedmodules}" ]; then
return 1 #false
fi
for loadedmodule in ${loadedmodules} ; do
if [ "$1" == "$loadedmodule" ]; then
modprobe -r $1
break
fi