-
Notifications
You must be signed in to change notification settings - Fork 3
/
ipc.c
95 lines (86 loc) · 1.81 KB
/
ipc.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
#include "ipc.h"
int connect_socket(int *socket_fd)
{
int len;
struct sockaddr_un sock_addr;
int enable = 1;
if ((*socket_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
perror("socket");
}
if (setsockopt(*socket_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
perror("setsockopt(SO_REUSEADDR) failed");
memset(&sock_addr, 0, sizeof(sock_addr));
sock_addr.sun_family = AF_UNIX;
strcpy(sock_addr.sun_path, SOCKET_FILE);
if (connect(*socket_fd, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) < 0)
{
perror("connect");
return 0;
}
else
{
printf("Connected to socket!\n");
return 1;
}
}
int socket_send_data(int *socket_fd, char *buff)
{
if (send(*socket_fd, buff, strlen(buff)+1, 0) == -1) {
perror("SendData Failed!");
return 0;
}
else
{
//printf("Sent Data\n");
return 1;
}
}
char *make_json(
float active,
float reactive,
float powerl1,
float current,
float voltage,
float phaseanglecurrentvoltagel1
)
{
char *string = NULL;
cJSON *root = cJSON_CreateObject();
cJSON_AddItemToObject(
root,
"active",
cJSON_CreateNumber(active)
);
cJSON_AddItemToObject(
root,
"reactive",
cJSON_CreateNumber(reactive)
);
cJSON_AddItemToObject(
root,
"powerl1",
cJSON_CreateNumber(powerl1)
);
cJSON_AddItemToObject(
root,
"current",
cJSON_CreateNumber(current)
);
cJSON_AddItemToObject(
root,
"voltage",
cJSON_CreateNumber(voltage)
);
cJSON_AddItemToObject(
root,
"phaseanglecurrentvoltagel1",
cJSON_CreateNumber(phaseanglecurrentvoltagel1)
);
string = cJSON_Print(root);
if (string == NULL) {
fprintf(stderr, "Failed to print monitor.\n");
}
/* free all objects under root and root itself */
cJSON_Delete(root);
return string;
}