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 Vincent Wochnik

Committer:
xinlei
Date:
Mon Aug 08 11:05:57 2016 +0000
Revision:
139:f8ab852e83e7
Parent:
135:c4009ecaf5c0
Etisalat and Teleena APN.

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 135:c4009ecaf5c0 6
xinlei 99:e369fc75c000 7 GPSTracker::GPSTracker() :
xinlei 99:e369fc75c000 8 _gps(),
Cumulocity 47:89ae46d5c466 9 _thread(GPSTracker::thread_func, this),
Cumulocity 47:89ae46d5c466 10 _positionSet(false)
Cumulocity 47:89ae46d5c466 11 {
xinlei 99:e369fc75c000 12 _gps.init();
Cumulocity 47:89ae46d5c466 13 }
Cumulocity 47:89ae46d5c466 14
xinlei 135:c4009ecaf5c0 15
Cumulocity 47:89ae46d5c466 16 bool GPSTracker::position(GPSTracker::Position *position)
Cumulocity 47:89ae46d5c466 17 {
xinlei 131:ca312ec4bd0f 18 bool result = false;
Cumulocity 47:89ae46d5c466 19
Cumulocity 47:89ae46d5c466 20 _mutex.lock();
Cumulocity 47:89ae46d5c466 21 if (_positionSet) {
Cumulocity 47:89ae46d5c466 22 memcpy(position, &_position, sizeof(GPSTracker::Position));
Cumulocity 47:89ae46d5c466 23 _positionSet = false;
Cumulocity 47:89ae46d5c466 24 result = true;
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
xinlei 135:c4009ecaf5c0 31
Cumulocity 47:89ae46d5c466 32 void GPSTracker::thread()
Cumulocity 47:89ae46d5c466 33 {
Cumulocity 47:89ae46d5c466 34 char buf[256], chr; // needs to be that big otherwise mdm isn't working
Cumulocity 47:89ae46d5c466 35 int ret, len, n;
Cumulocity 47:89ae46d5c466 36 double altitude, latitude, longitude;
Cumulocity 47:89ae46d5c466 37 while (true) {
Cumulocity 47:89ae46d5c466 38 ret = _gps.getMessage(buf, sizeof(buf));
Cumulocity 47:89ae46d5c466 39 if (ret <= 0) {
Cumulocity 47:89ae46d5c466 40 Thread::wait(100);
Cumulocity 47:89ae46d5c466 41 continue;
xinlei 131:ca312ec4bd0f 42 }
Cumulocity 47:89ae46d5c466 43 len = LENGTH(ret);
xinlei 76:b07effe83fb8 44 if ((PROTOCOL(ret) != GPSParser::NMEA) || (len <= 6)) {
xinlei 135:c4009ecaf5c0 45 aWarning("GPS data is not in NMEA protocol!\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 131:ca312ec4bd0f 50 if (strncmp("$GPGGA", buf, 6) != 0 && strncmp("$GNGGA", buf, 6) != 0)
Cumulocity 47:89ae46d5c466 51 continue;
xinlei 131:ca312ec4bd0f 52 if (!_gps.getNmeaItem(6, buf, len, n, 10) || n == 0)
xinlei 76:b07effe83fb8 53 continue;
Cumulocity 47:89ae46d5c466 54
Cumulocity 47:89ae46d5c466 55 // get altitude, latitude and longitude
Cumulocity 47:89ae46d5c466 56 if ((!_gps.getNmeaAngle(2, buf, len, latitude)) ||
Cumulocity 47:89ae46d5c466 57 (!_gps.getNmeaAngle(4, buf, len, longitude)) ||
Cumulocity 47:89ae46d5c466 58 (!_gps.getNmeaItem(9, buf, len, altitude)) ||
Cumulocity 47:89ae46d5c466 59 (!_gps.getNmeaItem(10, buf, len, chr)) ||
xinlei 76:b07effe83fb8 60 (chr != 'M')) {
Cumulocity 47:89ae46d5c466 61 continue;
xinlei 76:b07effe83fb8 62 }
Cumulocity 47:89ae46d5c466 63
Cumulocity 47:89ae46d5c466 64 _mutex.lock();
Cumulocity 47:89ae46d5c466 65 _position.altitude = altitude;
Cumulocity 47:89ae46d5c466 66 _position.latitude = latitude;
Cumulocity 47:89ae46d5c466 67 _position.longitude = longitude;
Cumulocity 47:89ae46d5c466 68 _positionSet = true;
Cumulocity 47:89ae46d5c466 69 _mutex.unlock();
Cumulocity 47:89ae46d5c466 70 }
Cumulocity 47:89ae46d5c466 71 }
Cumulocity 47:89ae46d5c466 72
xinlei 135:c4009ecaf5c0 73
Cumulocity 47:89ae46d5c466 74 void GPSTracker::thread_func(void const *arg)
Cumulocity 47:89ae46d5c466 75 {
Cumulocity 47:89ae46d5c466 76 GPSTracker *that;
Cumulocity 47:89ae46d5c466 77 that = (GPSTracker*)arg;
Cumulocity 47:89ae46d5c466 78 that->thread();
Cumulocity 47:89ae46d5c466 79 }