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:
Thu May 07 09:57:55 2015 +0000
Revision:
99:e369fc75c000
Parent:
93:0acd11870c6a
Child:
107:fc5f25f0e0d5
prepare for v2.1rc3.

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 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 {
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;
xinlei 93:0acd11870c6a 36 aInfo("GPS thread: %p\r\n", Thread::gettid());
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;
Cumulocity 47:89ae46d5c466 42 }
Cumulocity 47:89ae46d5c466 43
Cumulocity 47:89ae46d5c466 44 len = LENGTH(ret);
xinlei 76:b07effe83fb8 45 if ((PROTOCOL(ret) != GPSParser::NMEA) || (len <= 6)) {
xinlei 76:b07effe83fb8 46 aWarning("GPS data is not in NMEA protocol!\r\n");
Cumulocity 47:89ae46d5c466 47 continue;
xinlei 76:b07effe83fb8 48 }
Cumulocity 47:89ae46d5c466 49
xinlei 76:b07effe83fb8 50 // we're only interested in fixed GPS positions, data type: GPGGA
xinlei 76:b07effe83fb8 51 if ((strncmp("$GPGGA", buf, 6) != 0)) {
Cumulocity 47:89ae46d5c466 52 continue;
xinlei 76:b07effe83fb8 53 }
xinlei 76:b07effe83fb8 54
xinlei 76:b07effe83fb8 55 if (!_gps.getNmeaItem(6, buf, len, n, 10) || n == 0) {
xinlei 76:b07effe83fb8 56 continue;
xinlei 76:b07effe83fb8 57 }
Cumulocity 47:89ae46d5c466 58
Cumulocity 47:89ae46d5c466 59 // get altitude, latitude and longitude
Cumulocity 47:89ae46d5c466 60 if ((!_gps.getNmeaAngle(2, buf, len, latitude)) ||
Cumulocity 47:89ae46d5c466 61 (!_gps.getNmeaAngle(4, buf, len, longitude)) ||
Cumulocity 47:89ae46d5c466 62 (!_gps.getNmeaItem(9, buf, len, altitude)) ||
Cumulocity 47:89ae46d5c466 63 (!_gps.getNmeaItem(10, buf, len, chr)) ||
xinlei 76:b07effe83fb8 64 (chr != 'M')) {
xinlei 76:b07effe83fb8 65 aWarning("Unable to retrieve GPS data!\r\n");
Cumulocity 47:89ae46d5c466 66 continue;
xinlei 76:b07effe83fb8 67 }
Cumulocity 47:89ae46d5c466 68
Cumulocity 47:89ae46d5c466 69 _mutex.lock();
Cumulocity 47:89ae46d5c466 70 _position.altitude = altitude;
Cumulocity 47:89ae46d5c466 71 _position.latitude = latitude;
Cumulocity 47:89ae46d5c466 72 _position.longitude = longitude;
Cumulocity 47:89ae46d5c466 73 _positionSet = true;
Cumulocity 47:89ae46d5c466 74 _mutex.unlock();
Cumulocity 47:89ae46d5c466 75 }
Cumulocity 47:89ae46d5c466 76 }
Cumulocity 47:89ae46d5c466 77
Cumulocity 47:89ae46d5c466 78 void GPSTracker::thread_func(void const *arg)
Cumulocity 47:89ae46d5c466 79 {
Cumulocity 47:89ae46d5c466 80 GPSTracker *that;
Cumulocity 47:89ae46d5c466 81 that = (GPSTracker*)arg;
Cumulocity 47:89ae46d5c466 82 that->thread();
Cumulocity 47:89ae46d5c466 83 }