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

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

avoid buffer overrun

File size: 3.5 KB
RevLine 
[3]1// %flair:license{
[15]2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
[3]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
[15]26namespace flair {
27namespace sensor {
[3]28
[137]29Mb800::Mb800(string name,
[51]30 SerialPort *serialport, NmeaGps::NMEAFlags_t NMEAFlags,
[15]31 uint8_t priority)
[137]32 : NmeaGps(name, NMEAFlags), Thread(getFrameworkManager(), name, priority) {
[15]33 this->serialport = serialport;
34 serialport->SetRxTimeout(100000000);
[157]35
36 SetIsReady(true);
[3]37}
38
[15]39Mb800::~Mb800() {
40 SafeStop();
41 Join();
[3]42}
43
[15]44void Mb800::Run(void) {
[196]45 int written;
[3]46
[15]47 /** Debut config **/
48 char to_send[] = "$PASHS,NME,ALL,A,OFF\r\n";
[41]49 written=serialport->Write(to_send, sizeof(to_send));
50 if (written< 0) {
51 Thread::Err("erreur Write (%s)\n", strerror(-written));
52 }
[42]53
[15]54 {
55 char to_send[] = "$PASHS,CPD,AFP,95.0\r\n";
[41]56 written=serialport->Write(to_send, sizeof(to_send));
57 if (written< 0) {
58 Thread::Err("erreur Write (%s)\n", strerror(-written));
59 }
[15]60 }
61 {
62 char to_send[] = "$PASHS,DIF,PRT,C,RT3\r\n";
[41]63 written=serialport->Write(to_send, sizeof(to_send));
64 if (written< 0) {
65 Thread::Err("erreur Write (%s)\n", strerror(-written));
66 }
[15]67 }
[42]68
[15]69 if ((NMEAFlags & GGA) != 0) {
70 char to_send[] = "$PASHS,NME,GGA,A,ON,0.05\r\n";
[41]71 written = serialport->Write(to_send, sizeof(to_send));
72 if (written< 0) {
73 Thread::Err("erreur Write (%s)\n", strerror(-written));
74 }
[15]75 }
76 if ((NMEAFlags & VTG) != 0) {
77 char to_send[] = "$PASHS,NME,VTG,A,ON,0.05\r\n";
[41]78 written = serialport->Write(to_send, sizeof(to_send));
79 if (written< 0) {
80 Thread::Err("erreur Write (%s)\n", strerror(-written));
81 }
[15]82 }
83 if ((NMEAFlags & GST) != 0) {
84 char to_send[] = "$PASHS,NME,GST,A,ON,0.05\r\n";
[41]85 written = serialport->Write(to_send, sizeof(to_send));
86 if (written< 0) {
87 Thread::Err("erreur Write (%s)\n", strerror(-written));
88 }
[15]89 }
[180]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 }
[3]97
[15]98 Sync();
[3]99
[15]100 /** Fin config **/
[3]101
[15]102 /** Debut running loop **/
103 WarnUponSwitches(true);
[196]104
[15]105 while (!ToBeStopped()) {
[196]106 char response[1024] = {0};
107 int size = 0;
[15]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 }
[3]113
[196]114 if (response[size] == 0x0a ) break;
[15]115 size++;
[196]116 if (size==sizeof(response)) break;
[42]117 }
[196]118 if (size!=sizeof(response)) {
119 size++;
120 parseFrame(response, size);
121 } else {
122 Thread::Warn("frame too long for buffer\n");
123 }
[15]124 }
[196]125
[15]126 /** fin running loop **/
127 WarnUponSwitches(false);
[3]128}
129
[15]130void Mb800::Sync(void) {
131 char data = 0;
132 ssize_t read = 0;
[42]133
[15]134 // attente fin trame
[41]135 while (data != 0x0a && !ToBeStopped()) {
[15]136 read = serialport->Read(&data, 1);
[41]137 SleepMS(10);
[42]138
[15]139 if (read < 0) {
140 Thread::Err("erreur Read (%s)\n", strerror(-read));
[3]141 }
[15]142 }
[3]143}
144
145} // end namespace sensor
[170]146} // end namespace flair
Note: See TracBrowser for help on using the repository browser.