-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifx.c
110 lines (95 loc) · 3.25 KB
/
lifx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* Code inspired by this source:
* https://community.lifx.com/t/recvfrom-doesnt-answer-the-right-infos/912
* */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include "lifx.h"
#include "lifx-lib.h"
#define BUFF_SIZE 520
#define SERVER "172.16.0.255"
void sendMessage(int sock, struct sockaddr_in servaddr, char* buffer, size_t length)
{
//Sending
CHECK(sendto(sock, buffer, length, 0, (struct sockaddr*) &servaddr, sizeof(servaddr)), "Send problem");
printf("LIFX message sent!\n");
return;
}
void printMessage(char buffer[], int i)
{
printf("Response:\n");
for (i = 0; i < BUFF_SIZE; i++)
{
printf("%02X", buffer[i]);
}
printf("\n");
}
int main()
{
int sockfd;
int n;
int broadcast_enable=1;
struct sockaddr_in servaddr;
struct sockaddr_in client_addr;
socklen_t addr_len=sizeof(client_addr);
char buffer[BUFF_SIZE];
size_t length;
//Create socket
CHECK(sockfd=socket(AF_INET, SOCK_DGRAM,0), "DGRAM socket");
//enable Broadcast
setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcast_enable, sizeof(int) );
struct timeval tv = {
.tv_sec = 5
};
//set timeout to 5 sec
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, SERVER, &servaddr.sin_addr);
servaddr.sin_port = htons(PORT);
//power on
length = buildLIFX_PowerMessage(buffer, 1); //1 - on, 0 - off
sendMessage(sockfd, servaddr, buffer, length);
//get statistics
length = buildLIFX_GetPowerMessage(buffer); //Get power-status
sendMessage(sockfd, servaddr, buffer, length);
n = recvfrom(sockfd, (char *)buffer, BUFF_SIZE - 1,
MSG_WAITALL, (struct sockaddr *) &client_addr,
&addr_len);
printMessage(buffer,n);
length = buildLIFX_GetLightStatus(buffer); //Get light-status
sendMessage(sockfd, servaddr, buffer, length);
n = recvfrom(sockfd, (char *)buffer, BUFF_SIZE - 1,
MSG_WAITALL, (struct sockaddr *) &client_addr,
&addr_len);
printMessage(buffer,n);
length = buildLIFX_GetVersionMessage(buffer); //Get light-status
sendMessage(sockfd, servaddr, buffer, length);
n = recvfrom(sockfd, (char *)buffer, BUFF_SIZE - 1,
MSG_WAITALL, (struct sockaddr *) &client_addr,
&addr_len);
printMessage(buffer,n);
//set Color values
length = buildLIFX_ColorMessage(buffer, "red", 100); //in percent
sendMessage(sockfd, servaddr, buffer, length);
sleep(2);
length = buildLIFX_ColorMessage(buffer, "blue", 100); //in percent
sendMessage(sockfd, servaddr, buffer, length);
sleep(2);
length = buildLIFX_ColorMessage(buffer, "green", 100); //in percent
sendMessage(sockfd, servaddr, buffer, length);
sleep(2);
length = buildLIFX_ColorMessage(buffer, "magenta", 10); //in percent
sendMessage(sockfd, servaddr, buffer, length);
sleep(5);
length = buildLIFX_PowerMessage(buffer, 0); //1 - on, 0 - off
sendMessage(sockfd, servaddr, buffer, length);
close(sockfd);
return 0;
}