Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Wed May 13 12:12:55 2015 +0000
Revision:
106:fc5f25f0e0d5
Parent:
98:e369fc75c000
Child:
130:ca312ec4bd0f
OperationSupport revamped

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Cumulocity 47:89ae46d5c466 1 #include <stdlib.h>
Cumulocity 47:89ae46d5c466 2 #include <string.h>
xinlei 76:b07effe83fb8 3 #include "GPSTracker.h"
xinlei 76:b07effe83fb8 4 #include "logging.h"
Cumulocity 47:89ae46d5c466 5
xinlei 98:e369fc75c000 6 GPSTracker::GPSTracker() :
xinlei 98:e369fc75c000 7 _gps(),
Cumulocity 47:89ae46d5c466 8 _thread(GPSTracker::thread_func, this),
Cumulocity 47:89ae46d5c466 9 _positionSet(false)
Cumulocity 47:89ae46d5c466 10 {
xinlei 98:e369fc75c000 11 _gps.init();
Cumulocity 47:89ae46d5c466 12 }
Cumulocity 47:89ae46d5c466 13
Cumulocity 47:89ae46d5c466 14 bool GPSTracker::position(GPSTracker::Position *position)
Cumulocity 47:89ae46d5c466 15 {
Cumulocity 47:89ae46d5c466 16 bool result;
Cumulocity 47:89ae46d5c466 17
Cumulocity 47:89ae46d5c466 18 _mutex.lock();
Cumulocity 47:89ae46d5c466 19 if (_positionSet) {
Cumulocity 47:89ae46d5c466 20 memcpy(position, &_position, sizeof(GPSTracker::Position));
Cumulocity 47:89ae46d5c466 21 _positionSet = false;
Cumulocity 47:89ae46d5c466 22 result = true;
Cumulocity 47:89ae46d5c466 23 } else {
Cumulocity 47:89ae46d5c466 24 result = false;
Cumulocity 47:89ae46d5c466 25 }
Cumulocity 47:89ae46d5c466 26 _mutex.unlock();
Cumulocity 47:89ae46d5c466 27
Cumulocity 47:89ae46d5c466 28 return result;
Cumulocity 47:89ae46d5c466 29 }
vwochnik 60:3c822f97fc73 30
Cumulocity 47:89ae46d5c466 31 void GPSTracker::thread()
Cumulocity 47:89ae46d5c466 32 {
Cumulocity 47:89ae46d5c466 33 char buf[256], chr; // needs to be that big otherwise mdm isn't working
Cumulocity 47:89ae46d5c466 34 int ret, len, n;
Cumulocity 47:89ae46d5c466 35 double altitude, latitude, longitude;
Cumulocity 47:89ae46d5c466 36 while (true) {
Cumulocity 47:89ae46d5c466 37 ret = _gps.getMessage(buf, sizeof(buf));
Cumulocity 47:89ae46d5c466 38 if (ret <= 0) {
Cumulocity 47:89ae46d5c466 39 Thread::wait(100);
Cumulocity 47:89ae46d5c466 40 continue;
Cumulocity 47:89ae46d5c466 41 }
Cumulocity 47:89ae46d5c466 42
Cumulocity 47:89ae46d5c466 43 len = LENGTH(ret);
xinlei 76:b07effe83fb8 44 if ((PROTOCOL(ret) != GPSParser::NMEA) || (len <= 6)) {
xinlei 76:b07effe83fb8 45 aWarning("GPS data is not in NMEA protocol!\r\n");
Cumulocity 47:89ae46d5c466 46 continue;
xinlei 76:b07effe83fb8 47 }
Cumulocity 47:89ae46d5c466 48
xinlei 76:b07effe83fb8 49 // we're only interested in fixed GPS positions, data type: GPGGA
xinlei 76:b07effe83fb8 50 if ((strncmp("$GPGGA", buf, 6) != 0)) {
Cumulocity 47:89ae46d5c466 51 continue;
xinlei 76:b07effe83fb8 52 }
xinlei 76:b07effe83fb8 53
xinlei 76:b07effe83fb8 54 if (!_gps.getNmeaItem(6, buf, len, n, 10) || n == 0) {
xinlei 76:b07effe83fb8 55 continue;
xinlei 76:b07effe83fb8 56 }
Cumulocity 47:89ae46d5c466 57
Cumulocity 47:89ae46d5c466 58 // get altitude, latitude and longitude
Cumulocity 47:89ae46d5c466 59 if ((!_gps.getNmeaAngle(2, buf, len, latitude)) ||
Cumulocity 47:89ae46d5c466 60 (!_gps.getNmeaAngle(4, buf, len, longitude)) ||
Cumulocity 47:89ae46d5c466 61 (!_gps.getNmeaItem(9, buf, len, altitude)) ||
Cumulocity 47:89ae46d5c466 62 (!_gps.getNmeaItem(10, buf, len, chr)) ||
xinlei 76:b07effe83fb8 63 (chr != 'M')) {
xinlei 76:b07effe83fb8 64 aWarning("Unable to retrieve GPS data!\r\n");
Cumulocity 47:89ae46d5c466 65 continue;
xinlei 76:b07effe83fb8 66 }
Cumulocity 47:89ae46d5c466 67
Cumulocity 47:89ae46d5c466 68 _mutex.lock();
Cumulocity 47:89ae46d5c466 69 _position.altitude = altitude;
Cumulocity 47:89ae46d5c466 70 _position.latitude = latitude;
Cumulocity 47:89ae46d5c466 71 _position.longitude = longitude;
Cumulocity 47:89ae46d5c466 72 _positionSet = true;
Cumulocity 47:89ae46d5c466 73 _mutex.unlock();
Cumulocity 47:89ae46d5c466 74 }
Cumulocity 47:89ae46d5c466 75 }
Cumulocity 47:89ae46d5c466 76
Cumulocity 47:89ae46d5c466 77 void GPSTracker::thread_func(void const *arg)
Cumulocity 47:89ae46d5c466 78 {
Cumulocity 47:89ae46d5c466 79 GPSTracker *that;
Cumulocity 47:89ae46d5c466 80 that = (GPSTracker*)arg;
Cumulocity 47:89ae46d5c466 81 that->thread();
Cumulocity 47:89ae46d5c466 82 }