-
Notifications
You must be signed in to change notification settings - Fork 1
/
motor_api.c
158 lines (133 loc) · 2.98 KB
/
motor_api.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
#include "motor_api.h"
#include "motor.h"
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <unistd.h>
char motor_connect(MOTOR *motor, char *addr) {
int n;
struct hostent *server;
struct sockaddr_in serv_addr;
// Creating new socket for connection
if ((*motor = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Error on socket creation");
return FAIL;
}
// Getting host information in order to connect.
server = gethostbyname(addr);
if (server == NULL) {
fprintf(stderr, "Failed on hostname resolution\n");
return FAIL;
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *) server->h_addr, (char *) &serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(NC_PORT);
if (connect(*motor, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("Failed on motor connection");
return FAIL;
}
n = CONNECT+FROM_RCVR;
// Once the connection has been made, provide a node id to the connected server, and read its node id.
if (write(*motor, &n, 1) != 1) {
perror("Failed writing to motor");
return FAIL;
}
if (read(*motor, &n, 1) != 1) {
perror("Failed reading from motor");
return FAIL;
}
return n;
}
char motor_stop(MOTOR motor) {
int n;
n = STOPPED+FROM_RCVR;
if (write(motor, &n, 1) != 1) {
perror("Failed writing to motor");
return FAIL;
}
if (read(motor, &n, 1) != 1) {
perror("Failed reading from motor");
return FAIL;
}
return n;
}
char motor_forward(MOTOR motor) {
int n;
n = FORWARD+FROM_RCVR;
if (write(motor, &n, 1) != 1) {
perror("Failed writing to motor");
return FAIL;
}
if (read(motor, &n, 1) != 1) {
perror("Failed reading from motor");
return FAIL;
}
return n;
}
char motor_back(MOTOR motor) {
int n;
n = BACK+FROM_RCVR;
if (write(motor, &n, 1) != 1) {
perror("Failed writing to motor");
return FAIL;
}
if (read(motor, &n, 1) != 1) {
perror("Failed reading from motor");
return FAIL;
}
return n;
}
char motor_right(MOTOR motor) {
int n;
n = RIGHT+FROM_RCVR;
if (write(motor, &n, 1) != 1) {
perror("Failed writing to motor");
return FAIL;
}
if (read(motor, &n, 1) != 1) {
perror("Failed reading from motor");
return FAIL;
}
return n;
}
char motor_left(MOTOR motor) {
int n;
n = LEFT+FROM_RCVR;
if (write(motor, &n, 1) != 1) {
perror("Failed writing to motor");
return FAIL;
}
if (read(motor, &n, 1) != 1) {
perror("Failed reading from motor");
return FAIL;
}
return n;
}
char motor_state(MOTOR motor) {
int n;
n = STATE+FROM_RCVR;
if (write(motor, &n, 1) != 1) {
perror("Failed writing to motor");
return FAIL;
}
if (read(motor, &n, 1) != 1) {
perror("Failed reading from motor");
return FAIL;
}
return n;
}
char motor_disconnect(MOTOR *motor) {
int n;
n = DISCONNECT+FROM_RCVR;
if (write(*motor, &n, 1) != 1) {
perror("Failed writing to motor");
return FAIL;
}
if (read(*motor, &n, 1) != 1) {
perror("Failed reading from motor");
return FAIL;
}
close(*motor);
return n;
}