Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / lib / colonet_wireless / colonet_wireless.h @ 11

History | View | Annotate | Download (4.4 KB)

1 11 emarinel
/** @file colonet_wireless.h
2
 *
3
 * @brief Wireless library for communicating with colony robots
4
 *
5
 * @author Eugene Marinelli
6
 * @date 10/26/06
7
 *
8
 * @bug gtkterm must be running for the library to work...
9
 */
10
11
/*
12
   NOTES
13
   Things colonet server must be able to do with wireless:
14
   - Send a commmand/request packet to a specific robot
15
   - Accept packets from robots - need a "SIGNAL"-like function and a
16
   message queue
17
   - Only send stuff when we have the token
18
   - Pass token along to the next robot
19
   - Join ad-hoc network automatically
20
*/
21
22
#ifndef COLONET_WIRELESS_H_
23
#define COLONET_WIRELESS_H_
24
25
#include <queue>
26
#include <colonet_defs.h>
27
28
using namespace std;
29
30
typedef int(*MsgHandlerFunction)(ColonetPacket*);
31
/* takes src, msg_type, req_code, data */
32
33
class ColonetWireless {
34
 public:
35
  /** @brief Constructor - creates a new instance of ColonetWireless
36
   * and initializes values
37
   *
38
   * @param wl_port Either SERIAL_PORT or USB_PORT (as defined in COLONET_DEFS)
39
   * @param log_filename Path to log file - outputs all raw data on the network
40
   * If set to NULL, logging is disabled.
41
   * @param msg_handler Function to be called when a packet is received.
42
   * Must take a ColonetPacket as an argument (see defn of MsgHandlerFunction)
43
   * @param join_token_ring true if server should join the token ring,
44
   * false if it should just observe the network
45
   *
46
   * @return new ColonetWireless object
47
   */
48
  ColonetWireless(char* wl_port, MsgHandlerFunction message_handler_,
49
                  char* log_filename_, bool join_token_ring_,
50
                                                                        bool ignore_token_);
51
52
  /**
53
   * @brief Deconstructor - frees queue and other stuff
54
   */
55
  ~ColonetWireless(void);
56
57
  /** @brief Spawns a thread which reads data from the hardware interface
58
   * with the colony (either a dongle or a robot programmed to relay data)
59
   * and runs msg_handler when a full packet is received
60
   *
61
   * @return pointer to the thread
62
   */
63
  pthread_t* run_listener_thread(void);
64
65
  /** @brief Adds a message to the message sending queue.  One message in the
66
   * queue is sent each time the server receives the token.  Therefore
67
   * there might be a delay on the order of seconds before the packet is
68
   * actually sent.
69
   *
70
   * @param msg_dest ID of the robot message is being sent to.  To send to
71
   * all robots, use GLOBAL_DEST
72
   * @param msg_type Type of message being sent.  Should be either
73
   * COLONET_COMMAND or COLONET_REQUEST
74
   * @param msg_code The message code/id telling the robot what to do or
75
   * return.  For example this might be ORB_SET
76
   * @param data Pointer to a buffer of data to be sent.  Assumed to
77
   * point to valid data.  An invalid pointer will cause a segfault.
78
   *
79
   * @return -1 on failure, 0 on success
80
   */
81
  int send(unsigned char msg_dest, unsigned char msg_type,
82
           unsigned char msg_code, unsigned char* data);
83
84
  /** @brief Clears the message queue
85
   * @return void
86
   */
87
  void clear_message_queue(void);
88
89
  /** @brief Handles a complete packet.
90
   *
91
   * @param pkt A complete packet to which we should react
92
   *
93
   * @return 0 on success, -1 on failure
94
   */
95
  int handle_packet(ColonetPacket* pkt);
96
97
  //the rest of these would ideally be private, but must be accessed by
98
  //the listen function
99
  ColonetPacket* create_packet(unsigned char token_src_,
100
                               unsigned char token_dest_,
101
                               unsigned char msg_dest,
102
                               unsigned char msg_type,
103
                               unsigned char msg_code,
104
                               unsigned char* data, unsigned char num_robots_);
105
  ColonetPacket* build_packet(unsigned char* buf, int len);
106
  int send_packet(ColonetPacket* pkt);
107
108
  char wireless_port[80];
109
  unsigned char token_src; //id of server in token ring
110
  unsigned char token_dest; //id of "dest" robot in token ring
111
  unsigned char num_robots;
112
113
 private:
114
  queue<ColonetPacket*> outbox;
115
  bool join_token_ring;
116
  MsgHandlerFunction message_handler;
117
  bool have_token;
118
  bool connected; //whether we are connected to the robot
119
  bool first_packet_sent;
120
  bool logging_enabled;
121
        bool ignore_token;
122
  char log_filename[80];
123
124
  int transmit_queued_message(void);
125
  int join_network(ColonetPacket* pkt);
126
  void update_network_props(ColonetPacket* pkt);
127
  unsigned char compute_checksum(ColonetPacket* pkt);
128
  void print_packet(ColonetPacket* pkt);
129
130
  int handle_error(ColonetPacket* pkt);
131
  int log_packet(ColonetPacket* pkt);
132
};
133
134
#endif