root / trunk / code / projects / colonet / lib / colonet_wireless / colonet_wireless.cpp @ 303
History | View | Annotate | Download (5.95 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, |
47 |
int len);
|
48 |
static void unregister(void); |
49 |
static int log_packet(unsigned char* packet, int len); |
50 |
|
51 |
|
52 |
/**************************** Public functions *******************************/
|
53 |
void colonet_wl_init(char* wl_port_, MsgHandlerFunction message_handler_, |
54 |
char* log_filename_) {
|
55 |
if (log_filename) {
|
56 |
logging_enabled = true;
|
57 |
sprintf(log_filename, log_filename_); |
58 |
} |
59 |
|
60 |
strncpy(wl_port, wl_port_, 40);
|
61 |
|
62 |
message_handler = message_handler_; |
63 |
|
64 |
printf("Calling wl_init...\n");
|
65 |
wl_init(); |
66 |
printf("wl_init succeeded.\n");
|
67 |
|
68 |
wl_token_ring_register(); |
69 |
|
70 |
printf("Joining token ring...\n");
|
71 |
wl_token_ring_join(); |
72 |
printf("Joined token ring.\n");
|
73 |
} |
74 |
|
75 |
void colonet_wl_kill_listener_thread() {
|
76 |
pthread_kill(listener_thread, 9);
|
77 |
} |
78 |
|
79 |
int colonet_wl_run_listener_thread() {
|
80 |
dbg_printf("Spawning listener thread...\n");
|
81 |
|
82 |
if (pthread_create(&listener_thread, NULL, listen, NULL)) { |
83 |
perror("pthread_create");
|
84 |
return -1; |
85 |
} |
86 |
|
87 |
return 0; |
88 |
} |
89 |
|
90 |
void colonet_wl_send(short client_source, short dest, ColonetMessageType msg_type, unsigned char msg_code, |
91 |
unsigned char* args) { |
92 |
printf("colonet_wl_send: client_source:%d, dest:%d, msg_code:%d\n", client_source, dest, msg_code);
|
93 |
|
94 |
ColonetRobotServerPacket pkt; |
95 |
pkt.client_id = client_source; |
96 |
pkt.msg_code = msg_code; |
97 |
|
98 |
for (int i = 0; i < PACKET_DATA_LEN; i++) { |
99 |
pkt.data[i] = args[i]; |
100 |
} |
101 |
|
102 |
if (dest == GLOBAL_DEST) {
|
103 |
printf("sending to global dest\n");
|
104 |
|
105 |
wl_send_global_packet(COLONET_PACKET_GROUP_ID, (char)msg_type, (char*)(&pkt), sizeof(ColonetRobotServerPacket), 0); |
106 |
} else {
|
107 |
printf("sending to specific robot.\n");
|
108 |
wl_send_robot_to_robot_global_packet(COLONET_PACKET_GROUP_ID, (char)msg_type, (char*)(&pkt), |
109 |
sizeof(ColonetRobotServerPacket), dest, COLONET_RESPONSE_PACKET_FRAME_ID);
|
110 |
} |
111 |
} |
112 |
|
113 |
int colonet_get_num_robots(void) { |
114 |
return wl_token_get_num_robots();
|
115 |
} |
116 |
|
117 |
int* colonet_get_xbee_ids(int* numrobots) { |
118 |
int num_robots = wl_token_get_num_robots();
|
119 |
int* ids = (int*)malloc(num_robots * sizeof(int)); |
120 |
|
121 |
wl_token_iterator_begin(); |
122 |
|
123 |
int* p = ids;
|
124 |
while (wl_token_iterator_has_next()) {
|
125 |
*p = wl_token_iterator_next(); |
126 |
p++; |
127 |
} |
128 |
|
129 |
*numrobots = num_robots; |
130 |
return ids;
|
131 |
} |
132 |
|
133 |
// Returns int**; should be freed
|
134 |
int** colonet_get_sensor_matrix(int* numrobots, int** ids_) { |
135 |
int num_robots;
|
136 |
int* ids = colonet_get_xbee_ids(&num_robots);
|
137 |
|
138 |
int** m = (int**)malloc(num_robots * sizeof(int*)); |
139 |
for (int i = 0; i < num_robots; i++) { |
140 |
m[i] = (int*)malloc(num_robots * sizeof(int*)); |
141 |
} |
142 |
|
143 |
for (int i = 0; i < num_robots; i++) { |
144 |
for (int j = 0; j < num_robots; j++) { |
145 |
m[i][j] = wl_token_get_sensor_reading(ids[i], ids[j]); |
146 |
} |
147 |
} |
148 |
|
149 |
*numrobots = num_robots; |
150 |
*ids_ = ids; |
151 |
return m;
|
152 |
} |
153 |
|
154 |
/**************************** Private functions ******************************/
|
155 |
|
156 |
static void timeout_handler() { |
157 |
printf("colonet wireless - timeout!\n");
|
158 |
} |
159 |
|
160 |
static void handle_response(int frame, int received) { |
161 |
printf("handle response\n");
|
162 |
} |
163 |
|
164 |
static void handle_receive(char type, int source, unsigned char* packet, |
165 |
int len) {
|
166 |
ColonetRobotServerPacket pkt; |
167 |
pkt.client_id = packet[0] | (packet[1]<<8) | (packet[2]<<16) | |
168 |
(packet[3]<<24); //little endian |
169 |
|
170 |
memcpy(pkt.data, packet + 5, PACKET_DATA_LEN);
|
171 |
|
172 |
if (logging_enabled) {
|
173 |
log_packet(packet, len); |
174 |
} |
175 |
|
176 |
message_handler(type, source, pkt.client_id, packet, len); |
177 |
|
178 |
printf("handle receive\n");
|
179 |
} |
180 |
|
181 |
static void unregister(void) { |
182 |
printf("unregister\n");
|
183 |
} |
184 |
|
185 |
/** @brief Analogous to the "SIGNAL" or "ISR" function on the robots.
|
186 |
* Listens for bytes on the wireless port and constructs packets based on them.
|
187 |
* Not part of the ColonetWireless class since a function pointer must be
|
188 |
* passed in starting the thread that runs it (tricky or impossible if it's
|
189 |
* a class function).
|
190 |
*
|
191 |
* @param args Pointer to arguments. Can be safely casted to ListenerArgs type
|
192 |
* @return NULL
|
193 |
*/
|
194 |
static void* listen(void* args) { |
195 |
printf("Called listen.\n");
|
196 |
|
197 |
PacketGroupHandler pgh = {COLONET_PACKET_GROUP_ID, |
198 |
timeout_handler, |
199 |
handle_response, |
200 |
handle_receive, |
201 |
unregister}; |
202 |
wl_register_packet_group(&pgh); |
203 |
|
204 |
while (1) { |
205 |
wl_do(); |
206 |
} |
207 |
|
208 |
wl_terminate(); |
209 |
pthread_exit(NULL);
|
210 |
return NULL; |
211 |
} |
212 |
|
213 |
/** @brief
|
214 |
*
|
215 |
* @param pkt Packet to be logged
|
216 |
*
|
217 |
* @return 0 on success, -1 on failure
|
218 |
*/
|
219 |
int log_packet(unsigned char* packet, int len) { |
220 |
FILE* logfile = fopen(log_filename, "a");
|
221 |
|
222 |
if (logfile == NULL) { |
223 |
dbg_printf("%s: Error - fopen %s failed.\n", log_filename, __FUNCTION__);
|
224 |
return -1; |
225 |
} |
226 |
|
227 |
for (int i = 0; i < len; i++) { |
228 |
fprintf(logfile, "%d ", *((unsigned char*)packet + i)); |
229 |
} |
230 |
fprintf(logfile, "\n");
|
231 |
|
232 |
fclose(logfile); |
233 |
return 0; |
234 |
} |