-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevice.c
169 lines (140 loc) · 3.98 KB
/
device.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "device.h"
#include "settings.h"
#include "notice.h"
int getttyfd(const char *device, int speed);
int getsockfd(const char *host, const char *port);
int checktype(int type);
char format[256];
int device_new(struct device **device, const char *path)
{
struct device *dev = *device;
if ((dev = malloc(sizeof(struct device))) == NULL) {
perror("malloc");
return -1;
}
if ((dev->fd = getttyfd(path, speed)) < 0) {
free(dev);
return -1;
}
dev->fbufsize = 0;
dev->sock = -1;
sprintf(dev->sbuf, "%c%c%c", 129, 254, 128);
dev->sbufsize = 3;
dev->state = BEGIN;
if ((dev->path = malloc(strlen(path) + 1)) == NULL) {
perror("malloc");
close(dev->fd);
free(dev);
return -1;
}
strcpy(dev->path, path);
*device = dev;
return 0;
}
int device_connect(struct device *dev)
{
int i, port;
char service[128];
for (i = 0 ; i < dev->fbufsize ; i++) {
if (dev->state == WAITING_CONNECTION) {
break;
}
switch (dev->state) {
case BEGIN:
if (dev->fbuf[i] == 129) {
dev->state = PACKET_ID;
}
break;
case PACKET_ID:
if (dev->fbuf[i] == 255) {
dev->state = VAR_B;
} else {
dev->state = TYPE;
}
break;
case TYPE:
if (dev->fbuf[i] == 128) {
dev->state = BEGIN;
} else if (checktype(dev->fbuf[i])) {
dev->state = -(dev->fbuf[i] & 0xF);
} else {
dev->state = BEGIN;
}
break;
case VAR_B:
if (dev->fbuf[i] == 0x1) {
dev->state = DEV_ID;
} else {
dev->state = BEGIN;
}
break;
case DEV_ID:
dev->sock = dev->fbuf[i];
dev->state = END;
break;
case END:
if (dev->fbuf[i] == 128) {
dev->state = WAITING_CONNECTION;
} else if (checktype(dev->fbuf[i])) {
dev->state = -(dev->fbuf[i] & 0xF);
} else {
dev->state = BEGIN;
}
break;
default:
dev->state += 1;
}
}
memmove(dev->fbuf, dev->fbuf + i, (dev->fbufsize - i) * sizeof(*(dev->fbuf)));
dev->fbufsize -= i;
if (dev->state != WAITING_CONNECTION)
return 0;
if (!(port = getportbyid(dev->sock))) {
fprintf(stderr, "getportbyid: no port associated with this id (%d)\n", dev->sock);
//notice_id(IDFAIL, dev->path, dev->sock);
return -1;
}
sprintf(service, "%d", port);
if ((dev->sock = getsockfd(host, service)) < 0) {
notice_sock(SOCKFAIL, dev->path, host, service);
return -1;
}
dev->state = CONNECTED;
notice_sock(CONNECT, dev->path, host, service);
return 0;
}
int device_read(int fd, unsigned char *buffer, int *bufsize)
{
int readed;
if ((readed = read(fd, buffer + *bufsize, BUF_SIZE - *bufsize)) <= 0) {
return -1;
}
*bufsize += readed;
return 0;
}
int device_write(int fd, unsigned char *buffer, int *bufsize)
{
int written;
if ((written = write(fd, buffer, *bufsize)) <= 0) {
return -1;
}
memmove(buffer, buffer + written, (*bufsize - written) * sizeof(*buffer));
*bufsize -= written;
return 0;
}
void device_delete(struct device **device)
{
struct device *dev = *device;
// Fermeture des fd
close(dev->fd);
if (dev->state == CONNECTED) {
close(dev->sock);
}
// Libération des ressources alloué dans device_new
free(dev->path);
free(dev);
*device = NULL;
}