-
Notifications
You must be signed in to change notification settings - Fork 0
/
elev.c
125 lines (91 loc) · 2.69 KB
/
elev.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
#include <assert.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <pthread.h>
#include "elev.h"
static int sockfd;
static pthread_mutex_t sockmtx;
int elev_init() {
char ip[16] = "localhost";
char port[8] = "15657";
pthread_mutex_init(&sockmtx, NULL);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
assert(sockfd != -1 && "Unable to set up socket");
struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
.ai_protocol = IPPROTO_TCP,
};
struct addrinfo* res;
getaddrinfo(ip, port, &hints, &res);
int fail = connect(sockfd, res->ai_addr, res->ai_addrlen);
assert(fail == 0 && "Unable to connect to simulator server");
freeaddrinfo(res);
send(sockfd, (char[4]) { 0 }, 4, 0);
return 1; //jalla
}
void elev_set_motor_direction(elev_motor_direction_t dirn) {
pthread_mutex_lock(&sockmtx);
send(sockfd, (char[4]) { 1, dirn }, 4, 0);
pthread_mutex_unlock(&sockmtx);
}
void elev_set_button_lamp(elev_button_type_t button, int floor, int value) {
assert(floor >= 0);
assert(floor < N_FLOORS);
assert(button >= 0);
assert(button < N_BUTTONS);
pthread_mutex_lock(&sockmtx);
send(sockfd, (char[4]) { 2, button, floor, value }, 4, 0);
pthread_mutex_unlock(&sockmtx);
}
void elev_set_floor_indicator(int floor) {
assert(floor >= 0);
assert(floor < N_FLOORS);
pthread_mutex_lock(&sockmtx);
send(sockfd, (char[4]) { 3, floor }, 4, 0);
pthread_mutex_unlock(&sockmtx);
}
void elev_set_door_open_lamp(int value) {
pthread_mutex_lock(&sockmtx);
send(sockfd, (char[4]) { 4, value }, 4, 0);
pthread_mutex_unlock(&sockmtx);
}
void elev_set_stop_lamp(int value) {
pthread_mutex_lock(&sockmtx);
send(sockfd, (char[4]) { 5, value }, 4, 0);
pthread_mutex_unlock(&sockmtx);
}
int elev_get_button_signal(elev_button_type_t button, int floor) {
pthread_mutex_lock(&sockmtx);
send(sockfd, (char[4]) { 6, button, floor }, 4, 0);
char buf[4];
recv(sockfd, buf, 4, 0);
pthread_mutex_unlock(&sockmtx);
return buf[1];
}
int elev_get_floor_sensor_signal(void) {
pthread_mutex_lock(&sockmtx);
send(sockfd, (char[4]) { 7 }, 4, 0);
char buf[4];
recv(sockfd, buf, 4, 0);
pthread_mutex_unlock(&sockmtx);
return buf[1] ? buf[2] : -1;
}
int elev_get_stop_signal(void) {
pthread_mutex_lock(&sockmtx);
send(sockfd, (char[4]) { 8 }, 4, 0);
char buf[4];
recv(sockfd, buf, 4, 0);
pthread_mutex_unlock(&sockmtx);
return buf[1];
}
int elev_get_obstruction_signal(void) {
pthread_mutex_lock(&sockmtx);
send(sockfd, (char[4]) { 9 }, 4, 0);
char buf[4];
recv(sockfd, buf, 4, 0);
pthread_mutex_unlock(&sockmtx);
return buf[1];
}