Hello,
I ran into this issue today where I couldnt use some of basic libraries of C++ such as iostream.h and sstream.h while writing application. My application requires concatinating float data and string data hence these are necessary to me. Can anybody tell me how can I utilise these libraries while building the app? Thansk in advance.
#include "interfaces.h"
#include "legato.h"
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include <iostream.h>
// Used to convert GPS int to double
#define GPS_DECIMAL_SHIFT 6
// Used for distance calculations
#define MIN_REQUIRED_HORIZ_ACCURACY_METRES 10 // TODO validate that this is realistic
#define POLL_PERIOD_SEC 2 * 60 // 2 minutes
#define RETRY_PERIOD_SEC 1
static le_posCtrl_ActivationRef_t posCtrlRef;
static le_timer_Ref_t pollingTimer;
static struct {
double lat;
double lon;
double horizAccuracy;
uint64_t datetime;
} lastReading;
/*** Determine if we have a reading * * (other things make factor in here down the road)*/
static bool hasReading() {
return lastReading.datetime != 0;
}
/**
* Determine if we can provide an IPC caller
* with a location
*/
static bool canGetLocation() {
return hasReading() && posCtrlRef != NULL;
}
/**
* IPC function to get location
*/
le_result_t brnkl_gps_getCurrentLocation(double* latitude,
double* longitude,
double* horizontalAccuracy,
uint64_t* readingTimestamp) {
if (!canGetLocation()) {
return LE_UNAVAILABLE;
}
*latitude = lastReading.lat;
*longitude = lastReading.lon;
*horizontalAccuracy = lastReading.horizAccuracy;
*readingTimestamp = lastReading.datetime;
return LE_OK;
}
/**
* Main polling function
*
* Change MIN_REQUIRED_HORIZ_ACCURACY_METRES if
* a more/less accurate fix is required
*/
int open_uart1 ( char *dev)
{
int fd;
fd = open (dev, O_RDWR | O_NOCTTY | O_NDELAY);
struct termios options;
// The old way. Let's not change baud settings
fcntl (fd, F_SETFL, 0);
// get the parameters
tcgetattr (fd, &options);
// Set the baud rates to 115200...
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);
// No parity (8N1):
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
// enable hardware flow control (CNEW_RTCCTS)
// options.c_cflag |= CRTSCTS;
// if(hw_handshake)
// Disable the hardware flow control for use with mangOH RED
options.c_cflag &= ~CRTSCTS;
// set raw input
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_iflag &= ~(INLCR | ICRNL | IGNCR);
// set raw output
options.c_oflag &= ~OPOST;
options.c_oflag &= ~OLCUC;
options.c_oflag &= ~ONLRET;
options.c_oflag &= ~ONOCR;
options.c_oflag &= ~OCRNL;
// Set the new options for the port...
tcsetattr (fd, TCSANOW, &options);
return fd;
}
void write_uart1 (int fd,char *cmd)
{
// fd_set rfds;
// struct timeval timeout;
int wrote = 0;
wrote = write (fd, cmd, strlen (cmd));
LE_INFO("wrote %d ",wrote);
}
static void getLocation(le_timer_Ref_t timerRef) {
le_timer_Stop(timerRef);
char myString;
int serial_fd;
char north="N";
char east="E";
serial_fd= open_uart1("/dev/ttyHS0");
LE_DEBUG("Checking GPS position");
int32_t rawLat, rawLon, rawHoriz;
le_result_t result = le_pos_Get2DLocation(&rawLat, &rawLon, &rawHoriz);
bool isAccurate = rawHoriz <= MIN_REQUIRED_HORIZ_ACCURACY_METRES;
bool resOk = result == LE_OK;
if (resOk && isAccurate) {
double denom = powf(10, GPS_DECIMAL_SHIFT); // divide by this
lastReading.lat = ((double)rawLat) / denom;
lastReading.lon = ((double)rawLon) / denom;
// no conversion required for horizontal accuracy
//lastReading.horizAccuracy = (double)rawHoriz;
//lastReading.datetime = GetCurrentTimestamp();
LE_INFO("Got reading...");
LE_INFO("lat: %f, long: %f, horiz: %f",
lastReading.lat, lastReading.lon, lastReading.horizAccuracy);
le_timer_SetMsInterval(timerRef, POLL_PERIOD_SEC * 1000);
} else {
if (!isAccurate && resOk) {
LE_INFO("Rejected for accuracy (%d m)", rawHoriz);
double denom = powf(10, GPS_DECIMAL_SHIFT); // divide by this
lastReading.lat = ((double)rawLat) / denom;
lastReading.lon = ((double)rawLon) / denom;
// no conversion required for horizontal accuracy
//lastReading.horizAccuracy = (double)rawHoriz;
//lastReading.datetime = GetCurrentTimestamp();
LE_INFO("Got reading...");
LE_INFO("%fN %fE , horiz: %f",
lastReading.lat, lastReading.lon, lastReading.horizAccuracy);
le_timer_SetMsInterval(timerRef, POLL_PERIOD_SEC * 1000);
myString= to_string(lastReading.lat)+ "N";
myString= myString + to_string(lastReading.lon)+ "E";
write_uart1(serial_fd,myString);
LE_INFO("Sent via UART0");
}
LE_INFO("Failed to get reading... retrying in %d seconds",
RETRY_PERIOD_SEC);
le_timer_SetMsInterval(timerRef, RETRY_PERIOD_SEC * 1000);
}
le_timer_Start(timerRef);
}
/**
* Perform all required setup
*
* Note that we run this on a timer to avoid
* blocking up the main (only) thread. If this
* was run in a while(true) that sleeps,
* the IPC caller would be blocked indefinitely
*/
static void gps_init() {
posCtrlRef = le_posCtrl_Request();
LE_FATAL_IF(posCtrlRef == NULL, "Couldn't activate positioning");
pollingTimer = le_timer_Create("GPS polling timer");
le_timer_SetHandler(pollingTimer, getLocation);
le_timer_SetRepeat(pollingTimer, 1);
le_timer_SetMsInterval(pollingTimer, 0);
le_timer_Start(pollingTimer);
}
COMPONENT_INIT {
gps_init();
}