commit ff94868a658c026397b095f0f14224e2ece49782 Author: Jason Benaim Date: Wed Sep 5 12:44:11 2018 -0700 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..51ff052 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +hs100 +*.o +*.swp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..68dc978 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +target ?= hs100 +objects := $(patsubst %.c,%.o,$(wildcard *.c)) +CFLAGS=-Wall -Werror -std=c99 -ggdb -Os + +.PHONY: all +all: $(target) + +.PHONY: clean +clean: + rm -f $(target) $(objects) + +$(target): $(objects) diff --git a/comms.c b/comms.c new file mode 100644 index 0000000..e4a09fe --- /dev/null +++ b/comms.c @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "comms.h" + +bool hs100_encrypt(uint8_t *d, uint8_t *s, size_t len) +{ + if(d == NULL) + return false; + if(s == NULL) + return false; + if(len == 0) + return false; + + uint8_t key = 0xab; + for(size_t i=0; i +#include +#include +#include +#include "comms.h" + +struct cmd_alias_s { + char *alias; + char *command; + int end; +}; +struct cmd_alias_s cmd_aliases[] = { + { + .alias = "off", + .command = "{\"system\":{\"set_relay_state\":{\"state\":0}}}", + }, + { + .alias = "on", + .command = "{\"system\":{\"set_relay_state\":{\"state\":1}}}", + }, + { + .end = 1, + }, +}; + +char *get_cmd(char *needle) +{ + int cmds_index = 0; + while(!cmd_aliases[cmds_index].end) + { + if(!strcmp(cmd_aliases[cmds_index].alias, needle)) + return cmd_aliases[cmds_index].command; + cmds_index++; + } + return needle; +} + +int main(int argc, char *argv[]) +{ + char *plug_addr = argv[1]; + char *cmd = argv[2]; + + cmd = get_cmd(cmd); + + char *response = hs100_send(plug_addr, cmd); + + if(response == NULL) { + fprintf(stderr, "failed to send command\n"); + return 1; + } + + printf("%s\n", response); + + free(response); + return 0; +}