device_configuration.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (Apache 2.0)
3  *
4  * Copyright (c) 2019, The MITRE Corporation.
5  * All rights reserved.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * https://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Sections of this project contains content developed by The MITRE Corporation.
20  * If this code is used in a deployment or embedded within another project,
21  * it is requested that you send an email to opensource@mitre.org in order to
22  * let us know where this software is being used.
23  *********************************************************************/
24 
26 
27 // RS 232
28 #include "rs232.h"
29 
30 namespace kvh
31 {
32 
54  {
55  if (_packetsRequested.size() > MAXIMUM_PACKET_PERIODS)
56  {
57  return -1;
58  }
59  // Make permanent in case it has a hot reset, otherwise an error is likely
60  _packetPeriods.permanent = 1;
61  // Clear all exisiting packet periods and replace with new ones
62  _packetPeriods.clear_existing_packets = 1;
63 
64  std::set<packet_id_e> packetIdList; // To hold list of already added packet id's
65  // \todo Figure out what to do with duplicated and unsupported variables
66  int duplicated = 0;
67  int unsupported = 0;
68 
69  for (int i = 0; i < _packetsRequested.size(); i++)
70  {
71  std::pair<packet_id_e, int> packet = _packetsRequested[i];
72 
73  if (supportedPackets_.count(packet.first) == 0)
74  {
75  unsupported += 1;
76  continue;
77  }
78 
79  if (packetIdList.count(packet.first) > 0)
80  {
81  duplicated += 1; // Found duplicate, increase counter
82  continue;
83  }
84 
85  // packet_period_t period = {packet id, period}
86  packet_period_t period;
87  period.packet_id = _packetsRequested[i].first;
88  period.period = 1000 / _packetsRequested[i].second; // Using formula for rate to period derived above
89  _packetPeriods.packet_periods[i] = period;
90 
91  // Record we have seen this packet id to look for duplicates
92  packetIdList.insert(packet.first);
93  }
94 
95  return unsupported + duplicated;
96  }
97 
115  filter_options_packet_t &_filterOptions,
116  bool _permanent,
117  uint8_t _vehicle_type,
118  bool _internal_gnss_enabled,
119  bool _atmospheric_altitude_enabled,
120  bool _velocity_heading_enabled,
121  bool _reversing_detection_enabled,
122  bool _motion_analysis_enabled)
123  {
124  if (_vehicle_type > 13)
125  {
126  return -1;
127  }
128 
129  _filterOptions.permanent = _permanent;
130  _filterOptions.vehicle_type = _vehicle_type;
131  _filterOptions.internal_gnss_enabled = _internal_gnss_enabled; // Set if we want to test using gps or not
132  _filterOptions.reserved = 0; // Need to set to 0, have found problems with other packets not doing this
133  _filterOptions.atmospheric_altitude_enabled = _atmospheric_altitude_enabled;
134  _filterOptions.velocity_heading_enabled = _velocity_heading_enabled;
135  _filterOptions.reversing_detection_enabled = _reversing_detection_enabled;
136  _filterOptions.motion_analysis_enabled = _motion_analysis_enabled;
137 
138  return 0;
139  }
140 
160  int KvhDeviceConfig::SetBaudRate(std::string _port, int _curBaudRate, int _primaryBaudRate, int _gpioBaudRate, int _auxBaudRate)
161  {
162  // Create the baud rate packet that we want to send
163  baud_rates_packet_t baudRatePacket;
164  baudRatePacket.permanent = 1;
165  baudRatePacket.primary_baud_rate = _primaryBaudRate;
166  baudRatePacket.gpio_1_2_baud_rate = _gpioBaudRate;
167  baudRatePacket.auxiliary_baud_rate = _auxBaudRate;
168  baudRatePacket.reserved = 0;
169 
170  an_packet_t *requestPacket = encode_baud_rates_packet(&baudRatePacket);
171 
172  char portArr[4096];
173  strncpy(portArr, _port.c_str(), 4096);
174  if (OpenComport(portArr, _curBaudRate) != 0)
175  {
176  return -1;
177  }
178 
179  an_packet_encode(requestPacket);
180  // Send AN packet via serial port
181  if (!SendBuf(an_packet_pointer(requestPacket), an_packet_size(requestPacket)))
182  {
183  return -2;
184  }
185  an_packet_free(&requestPacket);
186  requestPacket = nullptr;
187 
188  CloseComport();
189 
190  return 0;
191  }
192 
202  int KvhDeviceConfig::FindCurrentBaudRate(std::string _port, int _startingBaud)
203  {
204  std::vector<int> baudRates = {
205  1200, 1800, 2400, 4800, 9600,
206  19200, 57600, 115200, 230400,
207  460800, 500000, 576000, 921600, 1000000};
208 
209  char portArr[4096];
210  strncpy(portArr, _port.c_str(), 4096);
211 
212  for (int baudRate : baudRates)
213  {
214  // Skip over baud rates lower than what we want to start at
215  if (baudRate < _startingBaud)
216  {
217  continue;
218  }
219 
220  printf("Opening with port: %s, baudrate: %d...\n", portArr, baudRate);
221  if (OpenComport(portArr, baudRate) != 0)
222  {
223  continue; // Try next baud rate
224  }
225 
226  an_decoder_t anDecoder;
227  an_packet_t *anPacket;
228  an_decoder_initialise(&anDecoder);
229 
230  // Check for data over the next second
231  for (int i = 0; i < 20; i++)
232  {
233  int bytesRec = 0;
234 
235  // Check if new packets have been sent
236  if ((bytesRec = PollComport(an_decoder_pointer(&anDecoder), an_decoder_size(&anDecoder))) > 0)
237  {
238  an_decoder_increment(&anDecoder, bytesRec);
239  // See if we can decode a packet
240  while ((anPacket = an_packet_decode(&anDecoder)) != NULL)
241  {
242  // If so, we have found the correct baud rate
243  printf("Found a packet\n");
244  CloseComport();
245  an_packet_free(&anPacket);
246  printf("Found connection at %d baud\n", baudRate);
247  return baudRate;
248  }
249  }
250  usleep(10000);
251  }
252  CloseComport();
253  }
254  return -1;
255  }
256 
274  {
275  std::set<packet_id_e> packetIdList;
276  int dataThroughput = 0;
277 
278  for (int i = 0; i < _packetsRequested.size(); i++)
279  {
280  std::pair<packet_id_e, uint16_t> packet = _packetsRequested[i];
281 
282  // Check for duplicate packets or unsupported packet id's
283  if (packetSize_.count(packet.first) == 0)
284  {
285  return -1;
286  }
287  else if (packetIdList.count(packet.first) > 0)
288  {
289  return -2;
290  }
291 
292  // Max allowed hz is 1000
293  if (packet.second > 1000)
294  {
295  return -3;
296  }
297 
298  // Increase required baudrate by (struct_size + 5) * rate
299  dataThroughput += (packetSize_[packet.first] + 5) * packet.second;
300  packetIdList.insert(packet.first);
301  }
302 
303  return dataThroughput * 11; // Minimum baud = Data throughput * 11
304  }
305 
306 } // namespace kvh
static int SetBaudRate(std::string _port, int _curBaudRate, int _primaryBaudRate, int _gpioBaudRate=115200, int _auxBaudRate=115200)
This function can be used to set the buad rate independent of the other functions of the driver...
void an_decoder_initialise(an_decoder_t *an_decoder)
packet_period_t packet_periods[MAXIMUM_PACKET_PERIODS]
void CloseComport()
#define an_decoder_increment(an_decoder, bytes_received)
static int CreateFilterOptionsPacket(filter_options_packet_t &, bool _permanent=true, uint8_t _vehicle_type=vehicle_type_car, bool _internal_gnss_enabled=true, bool _atmospheric_altitude_enabled=true, bool _velocity_heading_enabled=true, bool _reversing_detection_enabled=true, bool _motion_analysis_enabled=true)
int OpenComport(char *, int)
std::set< packet_id_e > supportedPackets_
Set of packets containing all packet_id&#39;s we support.
static int CalculateRequiredBaud(KvhPacketRequest &)
Helper functions for configuring the hardware.
an_packet_t * an_packet_decode(an_decoder_t *an_decoder)
an_packet_t * encode_baud_rates_packet(baud_rates_packet_t *baud_rates_packet)
#define an_packet_size(packet)
#define MAXIMUM_PACKET_PERIODS
std::map< packet_id_e, int > packetSize_
Map relating packet id&#39;s to the associated struct size. Used for baudrate calculation.
static int FindCurrentBaudRate(std::string, int)
This function tries each possible setting of baudrates until it either finds one that is receiving pa...
int PollComport(unsigned char *, int)
std::vector< std::pair< packet_id_e, uint16_t > > KvhPacketRequest
#define an_decoder_size(an_decoder)
static int CreatePacketPeriodsPacket(KvhPacketRequest &_packetsRequested, packet_periods_packet_t &_packetPeriods)
void an_packet_encode(an_packet_t *an_packet)
#define an_packet_pointer(packet)
void an_packet_free(an_packet_t **an_packet)
int SendBuf(unsigned char *, int)
#define an_decoder_pointer(an_decoder)


kvh_geo_fog_3d_driver
Author(s): Trevor Bostic , Zach LaCelle
autogenerated on Fri Jan 24 2020 03:18:17