packet.cpp
Go to the documentation of this file.
1 /*
2  * P2OS for ROS
3  * Copyright (C) 2000 David Feil-Seifer, Brian Gerkey, Kasper Stoy,
4  * Richard Vaughan, & Andrew Howard
5  * Copyright (C) 2018 Hunter L. Allen
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  */
22 
23 #include <stdio.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <p2os_driver/packet.hpp>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <ros/ros.h>
30 
32 {
33  if (packet) {
34  ROS_INFO("\"");
35  for (int i = 0; i < size; i++) {
36  ROS_INFO("%u ", packet[i]);
37  }
38  ROS_INFO("\"");
39  }
40 }
41 
43 {
44  if (packet) {
45  ROS_INFO("\"");
46  for (int i = 0; i < size; i++) {
47  ROS_INFO("0x%.2x ", packet[i]);
48  }
49  ROS_INFO("\"");
50  }
51 }
52 
53 
55 {
56  const int16_t chksum = CalcChkSum();
57  return (chksum == (packet[size - 2] << 8)) | packet[size - 1];
58 }
59 
61 {
62  unsigned char * buffer = &packet[3];
63  int c = 0;
64  int n;
65 
66  for (n = size - 5; n > 1; ) {
67  c += (*(buffer) << 8) | *(buffer + 1);
68  c = c & 0xffff;
69  n -= 2;
70  buffer += 2;
71  }
72  if (n > 0) {
73  c ^= static_cast<int>(*(buffer++));
74  }
75 
76  return c;
77 }
78 
80 {
81  unsigned char prefix[3];
82  int cnt;
83 
84  ::memset(packet, 0, sizeof(packet));
85 
86  do {
87  ::memset(prefix, 0, sizeof(prefix));
88 
89  while (1) {
90  cnt = 0;
91  while (cnt != 1) {
92  if ((cnt += read(fd, &prefix[2], 1)) < 0) {
93  ROS_ERROR(
94  "Error reading packet.hppeader from robot connection: P2OSPacket():Receive():read():");
95  return 1;
96  }
97  }
98 
99  if (prefix[0] == 0xFA && prefix[1] == 0xFB) {
100  break;
101  }
102 
104 
105  // GlobalTime->GetTimeDouble(&timestamp);
106 
107  prefix[0] = prefix[1];
108  prefix[1] = prefix[2];
109  // skipped++;
110  }
111  // if (skipped>3) ROS_INFO("Skipped %d bytes\n", skipped);
112 
113  size = prefix[2] + 3;
114  memcpy(packet, prefix, 3);
115 
116  cnt = 0;
117  while (cnt != prefix[2]) {
118  if ((cnt += read(fd, &packet[3 + cnt], prefix[2] - cnt)) < 0) {
119  ROS_ERROR(
120  "Error reading packet body from robot connection: P2OSPacket():Receive():read():");
121  return 1;
122  }
123  }
124  } while (!Check());
125  return 0;
126 }
127 
128 int P2OSPacket::Build(unsigned char * data, unsigned char datasize)
129 {
130  int16_t chksum;
131 
132  size = datasize + 5;
133 
134  /* header */
135  packet[0] = 0xFA;
136  packet[1] = 0xFB;
137 
138  if (size > 198) {
139  ROS_ERROR("Packet to P2OS can't be larger than 200 bytes");
140  return 1;
141  }
142  packet[2] = datasize + 2;
143 
144  memcpy(&packet[3], data, datasize);
145 
146  chksum = CalcChkSum();
147  packet[3 + datasize] = chksum >> 8;
148  packet[3 + datasize + 1] = chksum & 0xFF;
149 
150  if (!Check()) {
151  ROS_ERROR("DAMN");
152  return 1;
153  }
154  return 0;
155 }
156 
157 int P2OSPacket::Send(int fd)
158 {
159  int cnt = 0;
160 
161  while (cnt != size) {
162  if ((cnt += write(fd, packet, size)) < 0) {
163  ROS_ERROR("Send");
164  return 1;
165  }
166  }
167  return 0;
168 }
void PrintHex()
Definition: packet.cpp:42
unsigned char packet[packet_len]
Definition: packet.hpp:36
void Print()
Definition: packet.cpp:31
ros::Time timestamp
Definition: packet.hpp:38
bool Check()
Definition: packet.cpp:54
int Build(unsigned char *data, unsigned char datasize)
Definition: packet.cpp:128
#define ROS_INFO(...)
int Receive(int fd)
Definition: packet.cpp:79
static Time now()
unsigned char size
Definition: packet.hpp:37
int CalcChkSum()
Definition: packet.cpp:60
int Send(int fd)
Definition: packet.cpp:157
#define ROS_ERROR(...)


p2os_driver
Author(s): Hunter L. Allen , David Feil-Seifer , Aris Synodinos , Brian Gerkey, Kasper Stoy, Richard Vaughan, Andrew Howard, Tucker Hermans, ActivMedia Robotics LLC, MobileRobots Inc.
autogenerated on Sat Jun 20 2020 03:29:42