You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.5 KiB

#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include "cfg.h"
#include "fbsh.h"
Config config;
FBSmartHome fbsh;
void help() {
printf ("fbsh-cli: parameters command options\n");
printf (" Client for accessing the fritzbox smart home devices.\n");
printf (" Most parameters can be set in a seperate config file.\n");
printf ("Commands:\n");
printf ("\thelp displays this helptext.\n");
printf ("\tconnect connect to the device, it will save the SID file.\n");
printf ("\tlist lists all devices, connection will be recovered from SID.\n");
printf ("\t If silent parameter is set, we will not be asked for credentials\n");
printf ("\tsaveconfig save configfile.\n");
printf ("\n");
printf ("Parameters:\n");
printf ("\t-config <CONFIGGILE> configfile to use\n");
printf ("\t-host <HOSTNAME> the fritzbox to connect to (http://fritz.box)\n");
printf ("\t-user <USERNAME> username to connect with\n");
printf ("\t-pass <PASSWORD> password to use\n");
printf ("\t-sidfile <SIDFILE> file which contains the session id\n");
printf ("\t-savepassword <0|1> should the password be saved in the configfile\n");
printf ("\n");
printf ("\t-silent <0|1> enable/disable: no questions about username or password\n");
printf ("\t-S enable silent mode\n");
printf ("\n");
exit (0);
}
void connect() {
fbsh.UseSIDFile(config.GetSIDFile());
if (fbsh.Connect(config.GetFBHostname()) != 0) {
if (fbsh.Connect(config.GetFBHostname(), config.GetUsername(), config.GetPassword()) != 0) {
fprintf (stderr, "could not connect to: %s Error:%s\n", config.GetFBHostname().c_str(), strerror(errno));
}
}
}
void getlist(int argpos, int argc, char **argv) {
std::list<FBSmartHomeDevice> *devlist;
std::list<FBSmartHomeDevice>::iterator iter;
connect ();
devlist = fbsh.GetDevices();
if (devlist == NULL) {
fprintf (stderr, "Error Retrieving Devices: %s\n", strerror(errno));
return;
}
for (iter = devlist->begin(); iter != devlist->end(); iter++) {
printf ("%s\t%s\t%s\t%s\n", iter->id.c_str(), iter->type.c_str(), iter->name.c_str(), iter->others.c_str());
}
delete devlist;
}
int main(int argc, char** argv) {
int i;
if (argc < 2) help();
else {
for (i = 1; i < argc; i++) {
//
// commands to run, all parameters must be set by here.
//
if (strcmp(argv[i], "help") == 0) {
help();
break;
}
else if (strcmp(argv[i], "connect") == 0) {
connect();
printf ("SID:%s\n", fbsh.GetSID().c_str());
break;
}
else if (strcmp(argv[i], "list") == 0) {
getlist(i, argc, argv);
break;
}
else if (strcmp(argv[i], "saveconfig") == 0) {
config.SaveConfig();
break;
}
//
// parameters will follow here
//
else if (strcmp(argv[i], "-host") == 0) {
config.SetFBHostname(argv[++i]);
}
else if (strcmp(argv[i], "-user") == 0) {
config.SetUsername(argv[++i]);
}
else if (strcmp(argv[i], "-pass") == 0) {
config.SetPassword(argv[++i]);
}
else if (strcmp(argv[i], "-silent") == 0) {
config.SetSilent(atoi(argv[++i]));
}
else if (strcmp(argv[i], "-S") == 0) {
config.SetSilent(true);
}
else if (strcmp(argv[i], "-sidfile") == 0) {
config.SetSIDFile(argv[++i]);
}
else if (strcmp(argv[i], "-savepassword") == 0) {
config.SetSavePassword(atoi(argv[++i]));
}
else if (strcmp(argv[i], "-config") == 0) {
config.SetFilename(argv[++i]);
config.LoadConfig();
}
}
}
return 0;
}