Statistics
| Branch: | Revision:

root / code / server / receive.c @ 9026523e

History | View | Annotate | Download (6.58 KB)

1
#include <termios.h>
2
#include <fcntl.h>
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <string.h>
6
#include <unistd.h>
7
#include <stdint.h>
8
#include <arpa/inet.h>
9
#include <netinet/in.h>
10
#include <netdb.h>
11
#include <signal.h>
12

    
13
//#define USE_SERVER
14

    
15
#define XBEE_PORT_DEFAULT "/dev/ttyUSB0" //or try USB1
16
#define XBEE_GET_PACKET_TIMEOUT 1000
17
#define XBEE_BUFFER_SIZE        128
18

    
19
#define SPOT_FREE  0
20
#define SPOT_TAKEN 1
21

    
22
static char* xbee_com_port = XBEE_PORT_DEFAULT;
23
static int xbee_stream;
24
char arrival_buf[XBEE_BUFFER_SIZE];
25
// location of last unread byte in buffer
26
volatile int buffer_last = 0;
27
// first unread byte in buffer
28
volatile int buffer_first = 0;
29
int server_sock;
30

    
31
char *server_hostname;
32
char *ip_addr;
33
struct in_addr server_addr;
34
int  port;
35
int  socket_open();
36

    
37
static int xbee_read(char* buf, int size);
38
static void* listen_to_xbee();
39
int xbee_lib_init();
40
void wl_relay_update();
41
void sigint_handler(int param);
42

    
43

    
44
int main(int argc, char *argv[]) {
45
   
46
        if (argc != 3) {
47
        fprintf(stderr, "USAGE: %s <server_ip> <port>\n", argv[0]);
48
        exit(1);
49
    }
50
    
51
    server_sock = 0;
52
    signal(SIGINT, sigint_handler);
53

    
54
    // resolve server hostname
55
    server_hostname = argv[1];
56
    struct hostent *my_host = gethostbyname(server_hostname);
57
    if (my_host == NULL) {
58
        printf("Failed to resolve hostname\n");
59
    }
60
    memcpy(&server_addr, my_host->h_addr_list[0], sizeof(struct in_addr));
61
    ip_addr = inet_ntoa(server_addr);
62
    printf("%s -> %s\n", server_hostname, ip_addr);
63

    
64
    // port on server
65
    port = atoi(argv[2]);
66
    
67
    // open connection to server
68
    #ifdef USE_SERVER
69
    server_sock = socket_open();
70
    #endif
71

    
72
    xbee_lib_init();
73
    while(1) {
74
            listen_to_xbee();
75
    }
76
        return 0;
77
}
78

    
79
int xbee_lib_init() {
80
        xbee_stream = open(xbee_com_port, O_RDWR);
81
        if (xbee_stream == -1) {
82
                printf("Failed to open connection to XBee on port ");
83
                printf("%i\n", *xbee_com_port);
84
                return -1;
85
        }
86
    else {
87
                  printf("Successfully opened connection to XBee on port ");
88
                printf("%i\n", (int)(*xbee_com_port));
89
        }
90
        // set baud rate, etc. correctly
91
        struct termios options;
92
        if(tcgetattr(xbee_stream, &options))
93
                printf("Error getting attributes");
94
        cfsetispeed(&options, B9600);
95
        cfsetospeed(&options, B9600);
96
        options.c_iflag &= ~ICRNL;
97
        options.c_oflag &= ~OCRNL;
98
        options.c_cflag |= (CLOCAL | CREAD);
99
        options.c_cflag &= ~PARENB;
100
        options.c_cflag &= ~CSTOPB;
101
        options.c_cflag &= ~CSIZE;
102
        options.c_cflag |= CS8;
103
        options.c_lflag &= ~ICANON;
104
        options.c_cc[VMIN] = 1;
105
        options.c_cc[VTIME] = 50;
106

    
107
        if (tcsetattr(xbee_stream, TCSANOW, &options)) {
108
                printf("Error setting attributes.\n");
109
                return -1;
110
        }
111

    
112
        return 0;
113
}
114

    
115
/**
116
 * Thread that listens to the xbee.
117
 **/
118
static void* listen_to_xbee() {
119
        char c;
120
        printf("Listening...\n");
121
        fflush(stdout);
122

    
123
    wl_relay_update();
124
        
125
    usleep(1000);
126

    
127
        return NULL;
128
}
129

    
130
static int xbee_read(char* buf, int size) {
131
        if (read(xbee_stream, buf, size) == -1) {
132
                printf("Failed to read from xbee.\r\n");
133
                return -1;
134
        }
135

    
136
        return 0;
137
}
138

    
139
void wl_relay_update() {
140
    // XBee API Mode Packet:
141
    // byte 0 = start delimiter (0x7E)
142
    // byte 1 = MSB of packet length (length of frame data)
143
    // byte 2 = LSB of packet length
144
    // byte 3 = API Identifier (0x81)
145
    // byte 4 = MSB of destination address
146
    // byte 5 = LSB of destination address
147
    // byte 6 = RSSI
148
    // byte 7 = Options (0x00)
149
    // byte 8 = Packet Type
150
    // byte 9 to n = RF Data (char*)
151
    //      9 = m_state
152
    //     10 = MSB of time_left
153
    //     11 = LSB of time_left
154
    //     12 = Button Status
155
    //     13 = Sonar Status
156
    // byte n+1 = checksum (add bytes 3 to n, subtract LSB from 0xFF)
157

    
158
    char     rec;
159
    uint16_t len;
160
    uint16_t src;
161
    uint8_t  rssi;
162
    uint8_t  m_state;
163
    uint16_t time_left;
164
    uint8_t  btn_status, sonar_status;
165
    uint8_t  checksum;
166

    
167
    int timeout_count = 0;
168

    
169
    // loop until we receive valid start byte
170
    xbee_read(&rec, 1);
171
    while (rec != 0x7E) {
172
        printf("Incorrect Start Delimiter\n");
173
        xbee_read(&rec, 1);
174
        timeout_count++;
175
        // try to reconnect to serial port:
176
        //  for when xbee is unplugged
177
        if (timeout_count > 500) {
178
            printf("are we unplugged?\n");
179
            timeout_count = 0;
180
            close(xbee_stream);
181
            xbee_lib_init();
182
        } 
183
    }
184

    
185
    xbee_read(&rec, 1);
186
    len = (uint8_t)rec << 8;
187
    xbee_read(&rec, 1);
188
    len |= (uint8_t)rec;
189

    
190
    char send_buf[len+4];
191
    send_buf[0] = 0x7E;
192
    send_buf[1] = ((uint8_t)len >> 8);
193
    send_buf[2] = (uint8_t)len;
194
    
195
    int i;
196
    for (i = 0; i <= len; i++) {
197
        xbee_read(&rec, 1);
198
        send_buf[i+3] = rec;
199
    }
200

    
201
    src = (send_buf[4] << 8) | send_buf[5];
202
    m_state = send_buf[9];
203
    time_left = ((uint8_t)send_buf[10] << 8) | (uint8_t)send_buf[11];
204
    //printf("len MSB: %x", (uint8_t)send_buf[10]);
205
    //printf(" len LSB: %x ", (uint8_t)send_buf[11]);
206

    
207
    btn_status = send_buf[12];
208
    sonar_status = send_buf[13];
209

    
210
    // sanity check - discard packet if it fails
211
    if ( (btn_status != 0 && btn_status != 1) ||
212
         (sonar_status != 0 && sonar_status != 1) ) {
213
        printf("bad packet from meter, not sent to server\n");
214
        return;
215
    }
216

    
217
    #ifdef USE_SERVER
218
    //int sock = socket_open();
219
    //printf("sending to server\n");
220
    if (send(server_sock, send_buf, len+4, 0) != len+4) {
221
            printf("Mismatch in number of sent bytes");
222
            //close(server_sock);
223
            return;
224
    }
225
    //close(sock);
226
    #endif
227

    
228
    printf("Meter %X is now ", src);
229
    if (m_state == SPOT_FREE) {
230
        printf("unoccupied. ");
231
    }
232
    else {
233
        printf ("occupied. ");
234
    }
235
    printf("There are %d seconds left. Button Status=%d. Sonar Status=%d\n",
236
           time_left, btn_status, sonar_status);
237
}
238

    
239
int socket_open() {
240
    int sock;
241
    struct sockaddr_in my_server;
242
    
243
    // Create the TCP socket
244
    if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
245
            printf("Failed to create socket\n");
246
            return -1;
247
    }
248
    // Construct the server sockaddr_in structure
249
    memset(&my_server, 0, sizeof(my_server));        // Clear struct
250
    my_server.sin_family = AF_INET;                  // Internet/IP
251
    my_server.sin_addr.s_addr = inet_addr(ip_addr);  // IP address
252
    my_server.sin_port = htons(port);                // server port
253
    // Establish connection
254
    if (connect(sock, (struct sockaddr *) &my_server, sizeof(my_server)) < 0) {
255
            printf("Failed to connect with server\n");
256
            return -1;
257
    }
258

    
259
    //printf("Socket created!?\n");
260

    
261
    return sock;
262
}
263

    
264
void sigint_handler(int param) {
265
    close(server_sock);
266
    printf("Relay node shutting down.\n");
267
    exit(0);
268
}
269