Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / ColonetServer / colonet_wireless.cpp @ 408

History | View | Annotate | Download (6.16 KB)

1
/** @file colonet_wireless.c
2
 *
3
 * @brief Implementation of server-side colonet wireless library
4
 *
5
 * @author Eugene Marinelli
6
 */
7

    
8
/********************************* Includes **********************************/
9
#include <string>
10
#include <pthread.h>
11
#include <iostream>
12
#include <fstream>
13
#include <unistd.h>
14
#include <fcntl.h>
15
#include <signal.h>
16

    
17
#include <wireless.h> // Colonet wireless library.
18
#include <wl_token_ring.h>
19

    
20
#include <colonet_defs.h>
21
#include <colonet_wireless.h>
22

    
23
/******************************* Definitions *********************************/
24

    
25
//Enable debug printouts
26
#define DEBUG 1
27

    
28
#ifdef DEBUG
29
#define dbg_printf(...) printf(__VA_ARGS__)
30
#define dbg_fprintf(...) fprintf(__VA_ARGS__)
31
#else
32
#define dbg_printf(...)
33
#define dbg_fprintf(...)
34
#endif
35

    
36
static MsgHandlerFunction message_handler;
37
static bool logging_enabled;
38
static char log_filename[80];
39
static pthread_t listener_thread;
40
static char wl_port[40];
41

    
42
/************************* Internal prototypes *******************************/
43
static void* listen(void* args);
44
static void timeout_handler(void);
45
static void handle_response(int frame, int received);
46
static void handle_receive(char type, int source, unsigned char* packet, int len);
47
static void unregister(void);
48
static int log_packet(unsigned char* packet, int len);
49

    
50
/**************************** Public functions *******************************/
51
int colonet_wl_init(char* wl_port_, MsgHandlerFunction message_handler_, char* log_filename_) {
52
  if (log_filename_ != NULL) {
53
    logging_enabled = true;
54
    strcpy(log_filename, log_filename_);
55
  }
56

    
57
  strncpy(wl_port, wl_port_, 40);
58

    
59
  message_handler = message_handler_;
60

    
61
  wl_set_com_port(wl_port);
62

    
63
  printf("Calling wl_init(%s)...\n", wl_port);
64
  if (wl_init() != 0) {
65
    fprintf(stderr, "wl_init failed.\n");
66
    return -1;
67
  }
68

    
69
  wl_token_ring_register();
70

    
71
  printf("Joining token ring...\n");
72
  if (wl_token_ring_join() != 0) {
73
    fprintf(stderr, "Failed to join token ring.\n");
74
    return -1;
75
  }
76
  printf("Joined token ring.\n");
77

    
78
  return 0;
79
}
80

    
81
void colonet_wl_kill_listener_thread() {
82
  pthread_kill(listener_thread, 9);
83
}
84

    
85
int colonet_wl_run_listener_thread() {
86
  dbg_printf("Spawning listener thread...\n");
87

    
88
  if (pthread_create(&listener_thread, NULL, listen, NULL)) {
89
    perror("pthread_create");
90
    return -1;
91
  }
92

    
93
  return 0;
94
}
95

    
96
int colonet_wl_send(short client_source, short dest, ColonetMessageType msg_type, unsigned char msg_code,
97
                     unsigned char* args) {
98
  printf("colonet_wl_send: client_source:%d, dest:%d, msg_code:%d\n", client_source, dest, msg_code);
99

    
100
  ColonetRobotServerPacket pkt;
101
  pkt.client_id = client_source;
102
  pkt.msg_code = msg_code;
103

    
104
  for (int i = 0; i < PACKET_DATA_LEN; i++) {
105
    pkt.data[i] = args[i];
106
  }
107

    
108
  if (dest == GLOBAL_DEST) {
109
    printf("sending to global dest\n");
110
    if (wl_send_global_packet(COLONET_PACKET_GROUP_ID, (char)msg_type, (char*)(&pkt),
111
                              sizeof(ColonetRobotServerPacket), 0) != 0) {
112
      return -1;
113
    }
114
  } else {
115
    printf("sending to specific robot.\n");
116
    wl_send_robot_to_robot_global_packet(COLONET_PACKET_GROUP_ID, (char)msg_type, (char*)(&pkt),
117
                                         sizeof(ColonetRobotServerPacket), dest, COLONET_RESPONSE_PACKET_FRAME_ID);
118
  }
119
}
120

    
121
int colonet_get_num_robots(void) {
122
  return wl_token_get_num_robots();
123
}
124

    
125
int* colonet_get_xbee_ids(int* numrobots) {
126
  int num_robots = wl_token_get_num_robots();
127
  int* ids = (int*)malloc(num_robots * sizeof(int));
128

    
129
  wl_token_iterator_begin();
130

    
131
  int* p = ids;
132
  while (wl_token_iterator_has_next()) {
133
    *p = wl_token_iterator_next();
134
    p++;
135
  }
136

    
137
  *numrobots = num_robots;
138
  return ids;
139
}
140

    
141
// Returns int**; should be freed
142
int** colonet_get_sensor_matrix(int* numrobots, int** ids_) {
143
  int num_robots;
144
  int* ids = colonet_get_xbee_ids(&num_robots);
145

    
146
  int** m = (int**)malloc(num_robots * sizeof(int*));
147
  for (int i = 0; i < num_robots; i++) {
148
    m[i] = (int*)malloc(num_robots * sizeof(int*));
149
  }
150

    
151
  for (int i = 0; i < num_robots; i++) {
152
    for (int j = 0; j < num_robots; j++) {
153
      m[i][j] = wl_token_get_sensor_reading(ids[i], ids[j]);
154
    }
155
  }
156

    
157
  *numrobots = num_robots;
158
  *ids_ = ids;
159
  return m;
160
}
161

    
162
/**************************** Private functions ******************************/
163

    
164
static void timeout_handler() {
165
  printf("colonet wireless - timeout!\n");
166
}
167

    
168
static void handle_response(int frame, int received) {
169
  printf("handle response\n");
170
}
171

    
172
static void handle_receive(char type, int source, unsigned char* packet,
173
                           int len) {
174
  ColonetRobotServerPacket pkt;
175
  pkt.client_id = packet[0] | (packet[1]<<8) | (packet[2]<<16) | (packet[3]<<24); //little endian
176

    
177
  memcpy(pkt.data, packet + 5, PACKET_DATA_LEN);
178

    
179
  if (logging_enabled) {
180
    log_packet(packet, len);
181
  }
182

    
183
  message_handler(type, source, pkt.client_id, packet, len);
184

    
185
  printf("handle receive\n");
186
}
187

    
188
static void unregister(void) {
189
  printf("unregister\n");
190
}
191

    
192
/** @brief Analogous to the "SIGNAL" or "ISR" function on the robots.
193
 * Listens for bytes on the wireless port and constructs packets based on them.
194
 * Not part of the ColonetWireless class since a function pointer must be
195
 * passed in starting the thread that runs it (tricky or impossible if it's
196
 * a class function).
197
 *
198
 * @param args Pointer to arguments.  Can be safely casted to ListenerArgs type
199
 * @return NULL
200
 */
201
static void* listen(void* args) {
202
  printf("Called listen.\n");
203

    
204
  PacketGroupHandler pgh = {COLONET_PACKET_GROUP_ID,
205
                            timeout_handler,
206
                            handle_response,
207
                            handle_receive,
208
                            unregister};
209
  wl_register_packet_group(&pgh);
210

    
211
  while (1) {
212
    wl_do();
213
    usleep(50000);
214
  }
215

    
216
  wl_terminate();
217
  pthread_exit(NULL);
218
  return NULL;
219
}
220

    
221
/** @brief
222
 *
223
 * @param pkt Packet to be logged
224
 *
225
 * @return 0 on success, -1 on failure
226
 */
227
int log_packet(unsigned char* packet, int len) {
228
  FILE* logfile = fopen(log_filename, "a");
229

    
230
  if (logfile == NULL) {
231
    dbg_printf("%s: Error - fopen %s failed.\n", log_filename, __FUNCTION__);
232
    return -1;
233
  }
234

    
235
  for (int i = 0; i < len; i++) {
236
    fprintf(logfile, "%d ", *((unsigned char*)packet + i));
237
  }
238
  fprintf(logfile, "\n");
239

    
240
  fclose(logfile);
241
  return 0;
242
}