From 602910d5d65304900866c38b13f69c1dcf98b81e Mon Sep 17 00:00:00 2001 From: scott park Date: Tue, 11 Jul 2017 23:16:01 +0900 Subject: [PATCH] The Initial GPS (berryGPS-IMU) HAL for RPI3 Implemented using PIO (Peripheral-io) - init, deinit, request (start-session, stop-session) API Change-Id: I3e761c8898bb1329151b80ed8cb79ed69fc06a58 --- CMakeLists.txt | 4 +- gps-plugin/src/gps_plugin_replay.c | 167 +++++++++++++++++++-- ...eplay.manifest => lbs-plugin-gps-rpi3.manifest} | 0 ...plugin-replay.spec => lbs-plugin-gps-rpi3.spec} | 17 ++- 4 files changed, 169 insertions(+), 19 deletions(-) rename packaging/{lbs-server-plugin-replay.manifest => lbs-plugin-gps-rpi3.manifest} (100%) rename packaging/{lbs-server-plugin-replay.spec => lbs-plugin-gps-rpi3.spec} (74%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b12a47..b1205b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -PROJECT(lbs-server-plugin-replay C) +PROJECT(lbs-plugin-gps-rpi3 C) SET(PREFIX ${CMAKE_INSTALL_PREFIX}) @@ -7,7 +7,7 @@ SET(PREFIX ${CMAKE_INSTALL_PREFIX}) # Set required packages INCLUDE(FindPkgConfig) -pkg_check_modules(pkgs REQUIRED glib-2.0 vconf dlog lbs-server-plugin libtzplatform-config) +pkg_check_modules(pkgs REQUIRED glib-2.0 vconf dlog lbs-server-plugin libtzplatform-config capi-system-peripheral-io) FOREACH(flag ${pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") diff --git a/gps-plugin/src/gps_plugin_replay.c b/gps-plugin/src/gps_plugin_replay.c index 6e24e77..86be4c0 100644 --- a/gps-plugin/src/gps_plugin_replay.c +++ b/gps-plugin/src/gps_plugin_replay.c @@ -35,13 +35,31 @@ #include "nmea_parser.h" #include "setting.h" +#include "peripheral_io.h" +#include "peripheral_gdbus_i2c.h" +#include "peripheral_common.h" +#include "peripheral_internal.h" + #define REPLAY_NMEA_SET_SIZE 4096 #define REPLAY_NMEA_SENTENCE_SIZE 128 +/* + * NMEA PACKET CMD for BerryGPS-IMU + */ +#define PMK_CMD_WAKEUP_MODE "$PMTK161,1*28\r\n" +#define PMK_CMD_STANDBY_MODE "$PMTK161,0*28\r\n" + +/* + * RPI3 UART PORT has only one. + */ +#define GPS_UART_PORT 0 +#define GPS_UART_BUFF_SIZE 1024 gps_event_cb g_gps_event_cb = NULL; void *g_user_data = NULL; +peripheral_uart_h uart_hndl = NULL; + typedef struct { FILE *fd; FILE *batch_fd; @@ -78,6 +96,70 @@ static const gps_plugin_interface g_gps_plugin_replay_interface = { gps_plugin_replay_gps_request }; +static int gps_uart_close() +{ + int ret; + + ret = peripheral_uart_close(uart_hndl); + if (ret < 0 ) { + _E("GPS Uart Port Close failed !!"); + return FALSE; + } + uart_hndl = NULL; + _D("GPS Uart Port Close Successful !!"); + return TRUE; +} + +static int gps_uart_open() +{ + int ret; + + ret = peripheral_uart_open(GPS_UART_PORT, &uart_hndl); + if (ret < 0 ) { + _E("GPS Uart Port Open failed !!"); + return FALSE; + } + _D("GPS Uart Port Open Successful !!"); + ret = peripheral_uart_set_baudrate(uart_hndl, PERIPHERAL_UART_BAUDRATE_9600); + if (ret < 0) { + _E("GPS Uart Port Open failed !!"); + goto ERROR; + } + ret = peripheral_uart_set_mode(uart_hndl, + PERIPHERAL_UART_BYTESIZE_8BIT, + PERIPHERAL_UART_PARITY_NONE, + PERIPHERAL_UART_STOPBITS_1BIT); + if (ret < 0) { + _E("GPS Uart Set Mode failed !!"); + goto ERROR; + } + ret = peripheral_uart_set_flowcontrol(uart_hndl, true, false); + if (ret < 0) { + _E("GPS Uart Set Flow Control Open failed !!"); + goto ERROR; + } + ret = peripheral_uart_flush(uart_hndl); + if (ret < 0) { + _E("GPS Uart Flush failed !!"); + goto ERROR; + } + return TRUE; +ERROR: + gps_uart_close(); + return FALSE; +} + +static int gps_uart_read(char *buf, int len) +{ + int length; + + /* ERROR if the length is less than 0 */ + if ((length = peripheral_uart_read(uart_hndl, (uint8_t *)buf, len)) < PERIPHERAL_ERROR_NONE) + _E("GPS Uart Read failed !!"); + + return length; +} + void gps_plugin_replay_pos_event(pos_data_t *data) { gps_event_info_t gps_event; @@ -279,6 +361,55 @@ void gps_plugin_respond_stop_session(void) g_gps_event_cb(&gps_event, g_user_data); } +gboolean gps_plugin_read_uart(replay_timeout *timer, char *nmea_data) +{ + char buf[GPS_UART_BUFF_SIZE] = { 0, }; + char *endstr = NULL; + int len = 0; + char *str = NULL; + + if (uart_hndl == NULL) { + LOG_PLUGIN(DBG_ERR, "uart handle is NULL"); + return FALSE; + } + + if (nmea_data == NULL) { + LOG_PLUGIN(DBG_ERR, "nmea_data is NULL"); + gps_uart_close(); + timer->fd = NULL; + return FALSE; + } + + if ((len = gps_uart_read(buf, GPS_UART_BUFF_SIZE)) < 0) { + LOG_PLUGIN(DBG_ERR, "gps_uart_read ERROR"); + return TRUE; + } + + /* remove uncompleted sentences */ + endstr = rindex(buf, '\n'); + if (endstr != NULL) + *endstr = '\0'; + else + return TRUE; + + str = (char *)strtok(buf, "\n"); + while (str != NULL) { + if (strncmp(str, "$GP", 3) == 0) { + if (strlen(nmea_data) + strlen(str) > REPLAY_NMEA_SET_SIZE) { + LOG_PLUGIN(DBG_ERR, "read nmea data size is too long"); + break; + } else { + strncpy(nmea_data + strlen(nmea_data), str, strlen(str) - 1); + } + timer->nmea_data->len = strlen(str); + timer->nmea_data->data = str; + gps_plugin_replay_nmea_event(timer->nmea_data); + } + str = (char *)strtok(NULL, "\n"); + } + return TRUE; +} + gboolean gps_plugin_replay_read_nmea(replay_timeout *timer, char *nmea_data) { gboolean ret = FALSE; @@ -314,8 +445,8 @@ gboolean gps_plugin_replay_read_nmea(replay_timeout *timer, char *nmea_data) LOG_PLUGIN(DBG_ERR, "read nmea data size is too long"); break; } else { - strncat(nmea_data, buf, strlen(buf)); - } + strncpy(nmea_data + strlen(nmea_data), buf, strlen(buf) - 1); + } } timer->nmea_data->len = strlen(buf); timer->nmea_data->data = buf; @@ -369,13 +500,13 @@ gboolean gps_plugin_replay_timeout_cb(gpointer data) memset(timer->sv_data, 0, sizeof(sv_data_t)); if (timer->replay_mode == REPLAY_NMEA) { - if (gps_plugin_replay_read_nmea(timer, nmea_data) == FALSE) { - LOG_PLUGIN(DBG_ERR, "Fail to read nmea data from file"); + if (gps_plugin_read_uart(timer, nmea_data) == FALSE) { + LOG_PLUGIN(DBG_ERR, "Fail to read nmea data from peripheral uart"); return FALSE; } else { err = nmea_parser(nmea_data, timer->pos_data, timer->sv_data); if (err == READ_ERROR) { - LOG_PLUGIN(DBG_ERR, "Fail to parser nmea data from file"); + LOG_PLUGIN(DBG_ERR, "Fail to parser nmea data from peripheral uart"); /*return FALSE; */ } else if (err == READ_NOT_FIXED) { LOG_PLUGIN(DBG_LOW, "GPS position is not fixed"); @@ -392,9 +523,19 @@ gboolean gps_plugin_replay_timeout_cb(gpointer data) err = READ_SUCCESS; } } else if (timer->replay_mode == REPLAY_OFF) { - LOG_PLUGIN(DBG_WARN, "replay_mode is OFF"); - err = READ_NOT_FIXED; - timer->sv_data->pos_valid = FALSE; + if (gps_plugin_replay_read_nmea(timer, nmea_data) == FALSE) { + LOG_PLUGIN(DBG_ERR, "Fail to read nmea data from file"); + return FALSE; + } else { + err = nmea_parser(nmea_data, timer->pos_data, timer->sv_data); + if (err == READ_ERROR) { + LOG_PLUGIN(DBG_ERR, "Fail to parser nmea data from file"); + /*return FALSE; */ + } else if (err == READ_NOT_FIXED) { + LOG_PLUGIN(DBG_LOW, "GPS position is not fixed"); + timer->sv_data->pos_valid = FALSE; + } + } } if (g_gps_event_cb != NULL) { @@ -414,7 +555,9 @@ gboolean gps_plugin_replay_timeout_cb(gpointer data) void gps_plugin_stop_replay_mode(replay_timeout *timer) { - if (timer->replay_mode == REPLAY_NMEA && fclose(timer->fd) != 0) + if (timer->replay_mode == REPLAY_NMEA && gps_uart_close() == FALSE) + LOG_PLUGIN(DBG_ERR, "peripheral_uart_close failed"); + else if (timer->replay_mode == REPLAY_OFF && fclose(timer->fd) != 0) LOG_PLUGIN(DBG_ERR, "fclose failed"); timer->fd = NULL; @@ -468,6 +611,9 @@ gboolean gps_plugin_start_replay_mode(replay_timeout *timer) gboolean ret = FALSE; if (timer->replay_mode == REPLAY_NMEA) { + if (gps_uart_open() == FALSE) + return FALSE; + } else if (timer->replay_mode == REPLAY_OFF) { if (gps_plugin_get_nmea_fd(timer) == FALSE) return FALSE; } @@ -687,6 +833,9 @@ int gps_plugin_replay_gps_deinit(gps_failure_reason_t *reason_code) g_gps_event_cb = NULL; g_user_data = NULL; + peripheral_uart_close(uart_hndl); + _D("GPS Uart Port Close Successful !!"); + return TRUE; } diff --git a/packaging/lbs-server-plugin-replay.manifest b/packaging/lbs-plugin-gps-rpi3.manifest similarity index 100% rename from packaging/lbs-server-plugin-replay.manifest rename to packaging/lbs-plugin-gps-rpi3.manifest diff --git a/packaging/lbs-server-plugin-replay.spec b/packaging/lbs-plugin-gps-rpi3.spec similarity index 74% rename from packaging/lbs-server-plugin-replay.spec rename to packaging/lbs-plugin-gps-rpi3.spec index 5eefd94..5baf97a 100644 --- a/packaging/lbs-server-plugin-replay.spec +++ b/packaging/lbs-plugin-gps-rpi3.spec @@ -1,17 +1,18 @@ -Name: lbs-server-plugin-replay +Name: lbs-plugin-gps-rpi3 Summary: LBS Server plugin library for replay mode -Version: 0.2.5 +Version: 0.1.0 Release: 1 Group: Location/Libraries License: Apache-2.0 Source0: %{name}-%{version}.tar.gz -Source1: lbs-server-plugin-replay.manifest +Source1: lbs-plugin-gps-rpi3.manifest BuildRequires: cmake BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(vconf) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(lbs-server-plugin) BuildRequires: pkgconfig(libtzplatform-config) +BuildRequires: pkgconfig(capi-system-peripheral-io) Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig @@ -47,17 +48,17 @@ cp -a nmea-log/*.log %{buildroot}%{DATADIR}/replay %post rm -rf %{_libdir}/liblbs-server-plugin.so -ln -sf %{_libdir}/liblbs-server-plugin-replay.so %{_libdir}/liblbs-server-plugin.so +ln -sf %{_libdir}/liblbs-plugin-gps-rpi3.so %{_libdir}/liblbs-server-plugin.so #for compatible with old version -ln -sf %{_libdir}/liblbs-server-plugin-replay.so %{_libdir}/libSLP-lbs-plugin-replay.so -ln -sf %{_libdir}/liblbs-server-plugin-replay.so %{_libdir}/libSLP-lbs-plugin.so +ln -sf %{_libdir}/liblbs-plugin-gps-rpi3.so %{_libdir}/libSLP-lbs-plugin-replay.so +ln -sf %{_libdir}/liblbs-plugin-gps-rpi3.so %{_libdir}/libSLP-lbs-plugin.so /sbin/ldconfig %postun -p /sbin/ldconfig %files -%manifest lbs-server-plugin-replay.manifest +%manifest lbs-plugin-gps-rpi3.manifest %license LICENSE %defattr(-,root,root,-) -%{_libdir}/liblbs-server-plugin-replay.so* +%{_libdir}/liblbs-plugin-gps-rpi3.so* %{DATADIR}/replay/* -- 2.7.4