source: flair-src/trunk/lib/FlairSensorActuator/src/Mb800.cpp@ 231

Last change on this file since 231 was 196, checked in by Sanahuja Guillaume, 7 years ago

avoid buffer overrun

File size: 3.5 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5// created: 2013/08/23
6// filename: Mb800.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: objet integrant le recepteur gps mb800
14//
15//
16/*********************************************************************/
17
18#include "Mb800.h"
19#include <SerialPort.h>
20#include <FrameworkManager.h>
21#include <string.h>
22
23using std::string;
24using namespace flair::core;
25
26namespace flair {
27namespace sensor {
28
29Mb800::Mb800(string name,
30 SerialPort *serialport, NmeaGps::NMEAFlags_t NMEAFlags,
31 uint8_t priority)
32 : NmeaGps(name, NMEAFlags), Thread(getFrameworkManager(), name, priority) {
33 this->serialport = serialport;
34 serialport->SetRxTimeout(100000000);
35
36 SetIsReady(true);
37}
38
39Mb800::~Mb800() {
40 SafeStop();
41 Join();
42}
43
44void Mb800::Run(void) {
45 int written;
46
47 /** Debut config **/
48 char to_send[] = "$PASHS,NME,ALL,A,OFF\r\n";
49 written=serialport->Write(to_send, sizeof(to_send));
50 if (written< 0) {
51 Thread::Err("erreur Write (%s)\n", strerror(-written));
52 }
53
54 {
55 char to_send[] = "$PASHS,CPD,AFP,95.0\r\n";
56 written=serialport->Write(to_send, sizeof(to_send));
57 if (written< 0) {
58 Thread::Err("erreur Write (%s)\n", strerror(-written));
59 }
60 }
61 {
62 char to_send[] = "$PASHS,DIF,PRT,C,RT3\r\n";
63 written=serialport->Write(to_send, sizeof(to_send));
64 if (written< 0) {
65 Thread::Err("erreur Write (%s)\n", strerror(-written));
66 }
67 }
68
69 if ((NMEAFlags & GGA) != 0) {
70 char to_send[] = "$PASHS,NME,GGA,A,ON,0.05\r\n";
71 written = serialport->Write(to_send, sizeof(to_send));
72 if (written< 0) {
73 Thread::Err("erreur Write (%s)\n", strerror(-written));
74 }
75 }
76 if ((NMEAFlags & VTG) != 0) {
77 char to_send[] = "$PASHS,NME,VTG,A,ON,0.05\r\n";
78 written = serialport->Write(to_send, sizeof(to_send));
79 if (written< 0) {
80 Thread::Err("erreur Write (%s)\n", strerror(-written));
81 }
82 }
83 if ((NMEAFlags & GST) != 0) {
84 char to_send[] = "$PASHS,NME,GST,A,ON,0.05\r\n";
85 written = serialport->Write(to_send, sizeof(to_send));
86 if (written< 0) {
87 Thread::Err("erreur Write (%s)\n", strerror(-written));
88 }
89 }
90 if ((NMEAFlags & GSA) != 0) {
91 char to_send[] = "$PASHS,NME,GSA,A,ON,0.1\r\n";
92 written = serialport->Write(to_send, sizeof(to_send));
93 if (written< 0) {
94 Thread::Err("erreur Write (%s)\n", strerror(-written));
95 }
96 }
97
98 Sync();
99
100 /** Fin config **/
101
102 /** Debut running loop **/
103 WarnUponSwitches(true);
104
105 while (!ToBeStopped()) {
106 char response[1024] = {0};
107 int size = 0;
108 while (!ToBeStopped()) {
109 ssize_t read = serialport->Read(&response[size], 1);
110 if (read < 0) {
111 Thread::Err("erreur Read (%s)\n", strerror(-read));
112 }
113
114 if (response[size] == 0x0a ) break;
115 size++;
116 if (size==sizeof(response)) break;
117 }
118 if (size!=sizeof(response)) {
119 size++;
120 parseFrame(response, size);
121 } else {
122 Thread::Warn("frame too long for buffer\n");
123 }
124 }
125
126 /** fin running loop **/
127 WarnUponSwitches(false);
128}
129
130void Mb800::Sync(void) {
131 char data = 0;
132 ssize_t read = 0;
133
134 // attente fin trame
135 while (data != 0x0a && !ToBeStopped()) {
136 read = serialport->Read(&data, 1);
137 SleepMS(10);
138
139 if (read < 0) {
140 Thread::Err("erreur Read (%s)\n", strerror(-read));
141 }
142 }
143}
144
145} // end namespace sensor
146} // end namespace flair
Note: See TracBrowser for help on using the repository browser.