Proof of concept to handle lights

This commit is contained in:
jpk 2017-08-27 17:36:18 +02:00
commit 7d6a8fecbe
2 changed files with 85 additions and 0 deletions

66
lw12.c Normal file
View File

@ -0,0 +1,66 @@
/*
* lw12.c
* Copyright (C) 2017 jpk <jpk@dwarf>
*
* Distributed under terms of the MIT license.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
//#include <netdb.h>
#include "lw12.h"
int lw12_connect(int *sockfd, struct sockaddr_in *addr, uint16_t port,
char *hostname) {
if ((*sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
memset((char *)addr, 0, sizeof(struct sockaddr_in));
addr->sin_family = AF_INET;
addr->sin_port = htons(port);
inet_aton(hostname, &addr->sin_addr);
return *sockfd;
}
int lw12_sendcmd(int sockfd, struct sockaddr_in *addr, char *cmd) {
const socklen_t slen = sizeof(struct sockaddr_in);
if (sendto(sockfd, cmd, LW12_CMD_LENGTH, 0, (const struct sockaddr *)addr,
slen) == -1) {
perror("sendto");
close(sockfd);
exit(EXIT_FAILURE);
}
return 0;
}
int main() {
int sockfd = 0;
struct sockaddr_in server_addr;
char serverip[] = "192.168.178.24";
uint16_t serverport = 5000;
lw12_connect(&sockfd, &server_addr, serverport, serverip);
lw12_sendcmd(sockfd, &server_addr, (char *)LIGHTS_ON);
usleep(2000000);
lw12_sendcmd(sockfd, &server_addr, (char *)LIGHTS_OFF);
close(sockfd);
return 0;
}

19
lw12.h Normal file
View File

@ -0,0 +1,19 @@
/*
* lw12.h
* Copyright (C) 2017 jpk <jpk@dwarf>
*
* Distributed under terms of the MIT license.
*/
#ifndef LW12_H
#define LW12_H
#define LW12_CMD_LENGTH 9
#define LIGHTS_ON "\x7e\x04\x04\x01\xff\xff\xff\x00\xef"
#define LIGHTS_OFF "\x7e\x04\x04\x00\x00\x0f\xff\x00\xef"
// set lights "7e070503{red:02x}{green:02x}{blue:02x}00ef"
#define LIGHT_COLOR "\x7\xe\x07\x05\x03\x00\x00\x00\x00\xef"
#endif /* !LW12_H */