Official reference client implementation for Cumulocity SmartREST on u-blox C027.
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
io/GPSTracker.cpp@131:ca312ec4bd0f, 2015-08-07 (annotated)
- Committer:
- xinlei
- Date:
- Fri Aug 07 12:38:28 2015 +0000
- Revision:
- 131:ca312ec4bd0f
- Parent:
- 107:fc5f25f0e0d5
- Child:
- 135:c4009ecaf5c0
GPSTracker: fix for new data format
Who changed what in which revision?
User | Revision | Line number | New 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 | 99:e369fc75c000 | 6 | GPSTracker::GPSTracker() : |
xinlei | 99:e369fc75c000 | 7 | _gps(), |
Cumulocity | 47:89ae46d5c466 | 8 | _thread(GPSTracker::thread_func, this), |
Cumulocity | 47:89ae46d5c466 | 9 | _positionSet(false) |
Cumulocity | 47:89ae46d5c466 | 10 | { |
xinlei | 99: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 | { |
xinlei | 131:ca312ec4bd0f | 16 | bool result = false; |
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 | } |
Cumulocity | 47:89ae46d5c466 | 24 | _mutex.unlock(); |
Cumulocity | 47:89ae46d5c466 | 25 | |
Cumulocity | 47:89ae46d5c466 | 26 | return result; |
Cumulocity | 47:89ae46d5c466 | 27 | } |
vwochnik | 60:3c822f97fc73 | 28 | |
Cumulocity | 47:89ae46d5c466 | 29 | void GPSTracker::thread() |
Cumulocity | 47:89ae46d5c466 | 30 | { |
Cumulocity | 47:89ae46d5c466 | 31 | char buf[256], chr; // needs to be that big otherwise mdm isn't working |
Cumulocity | 47:89ae46d5c466 | 32 | int ret, len, n; |
Cumulocity | 47:89ae46d5c466 | 33 | double altitude, latitude, longitude; |
Cumulocity | 47:89ae46d5c466 | 34 | while (true) { |
Cumulocity | 47:89ae46d5c466 | 35 | ret = _gps.getMessage(buf, sizeof(buf)); |
Cumulocity | 47:89ae46d5c466 | 36 | if (ret <= 0) { |
Cumulocity | 47:89ae46d5c466 | 37 | Thread::wait(100); |
Cumulocity | 47:89ae46d5c466 | 38 | continue; |
xinlei | 131:ca312ec4bd0f | 39 | } |
Cumulocity | 47:89ae46d5c466 | 40 | len = LENGTH(ret); |
xinlei | 76:b07effe83fb8 | 41 | if ((PROTOCOL(ret) != GPSParser::NMEA) || (len <= 6)) { |
xinlei | 76:b07effe83fb8 | 42 | aWarning("GPS data is not in NMEA protocol!\r\n"); |
Cumulocity | 47:89ae46d5c466 | 43 | continue; |
xinlei | 76:b07effe83fb8 | 44 | } |
Cumulocity | 47:89ae46d5c466 | 45 | |
xinlei | 76:b07effe83fb8 | 46 | // we're only interested in fixed GPS positions, data type: GPGGA |
xinlei | 131:ca312ec4bd0f | 47 | if (strncmp("$GPGGA", buf, 6) != 0 && strncmp("$GNGGA", buf, 6) != 0) |
Cumulocity | 47:89ae46d5c466 | 48 | continue; |
xinlei | 131:ca312ec4bd0f | 49 | if (!_gps.getNmeaItem(6, buf, len, n, 10) || n == 0) |
xinlei | 76:b07effe83fb8 | 50 | continue; |
Cumulocity | 47:89ae46d5c466 | 51 | |
Cumulocity | 47:89ae46d5c466 | 52 | // get altitude, latitude and longitude |
Cumulocity | 47:89ae46d5c466 | 53 | if ((!_gps.getNmeaAngle(2, buf, len, latitude)) || |
Cumulocity | 47:89ae46d5c466 | 54 | (!_gps.getNmeaAngle(4, buf, len, longitude)) || |
Cumulocity | 47:89ae46d5c466 | 55 | (!_gps.getNmeaItem(9, buf, len, altitude)) || |
Cumulocity | 47:89ae46d5c466 | 56 | (!_gps.getNmeaItem(10, buf, len, chr)) || |
xinlei | 76:b07effe83fb8 | 57 | (chr != 'M')) { |
Cumulocity | 47:89ae46d5c466 | 58 | continue; |
xinlei | 76:b07effe83fb8 | 59 | } |
Cumulocity | 47:89ae46d5c466 | 60 | |
Cumulocity | 47:89ae46d5c466 | 61 | _mutex.lock(); |
Cumulocity | 47:89ae46d5c466 | 62 | _position.altitude = altitude; |
Cumulocity | 47:89ae46d5c466 | 63 | _position.latitude = latitude; |
Cumulocity | 47:89ae46d5c466 | 64 | _position.longitude = longitude; |
Cumulocity | 47:89ae46d5c466 | 65 | _positionSet = true; |
Cumulocity | 47:89ae46d5c466 | 66 | _mutex.unlock(); |
Cumulocity | 47:89ae46d5c466 | 67 | } |
Cumulocity | 47:89ae46d5c466 | 68 | } |
Cumulocity | 47:89ae46d5c466 | 69 | |
Cumulocity | 47:89ae46d5c466 | 70 | void GPSTracker::thread_func(void const *arg) |
Cumulocity | 47:89ae46d5c466 | 71 | { |
Cumulocity | 47:89ae46d5c466 | 72 | GPSTracker *that; |
Cumulocity | 47:89ae46d5c466 | 73 | that = (GPSTracker*)arg; |
Cumulocity | 47:89ae46d5c466 | 74 | that->thread(); |
Cumulocity | 47:89ae46d5c466 | 75 | } |