|
| 1 | +/* |
| 2 | + * gpslib.h |
| 3 | + * |
| 4 | + */ |
| 5 | + |
| 6 | +#ifndef INC_GPSLIB_H_ |
| 7 | +#define INC_GPSLIB_H_ |
| 8 | + |
| 9 | +#include <time.h> |
| 10 | + |
| 11 | +/** |
| 12 | + * GPS Module type |
| 13 | + */ |
| 14 | +enum gpstype_e { |
| 15 | + GPS_TYPE_ANY, /**< GPS module that is not known to library */ |
| 16 | + GPS_TYPE_SIRF, /**< SIRF Star III GPS module */ |
| 17 | + GPS_TYPE_MTK, /**< GPS/GNSS module based on Mediatek chipset */ |
| 18 | + GPS_TYPE_STIRNSS,/**< GNSS module with IRNSS support based on STA80xx chipset (e.g. L89) */ |
| 19 | +}; |
| 20 | + |
| 21 | +/** |
| 22 | + * Geo-fence type |
| 23 | + */ |
| 24 | +enum geofence_type_e { |
| 25 | + FENCE_TYPE_CIRCLE,/**< Geo-fence of type circle */ |
| 26 | + FENCE_TYPE_POLY /**< Geo-fence of type polygon with more than 2 points */ |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * GPS library Events |
| 31 | + */ |
| 32 | +enum gpsevent_e { |
| 33 | + GPS_EVENT_FIRST_FIX, /**< GPS first fix event after system startup */ |
| 34 | + GPS_EVENT_MOTION_STOP, /**< Motion stop event */ |
| 35 | + GPS_EVENT_MOTION_START,/**< Motion start event. use @ref gps_get_starttrace |
| 36 | + function to get complete movement trace. */ |
| 37 | + GPS_EVENT_HARSHBREAK, /**< Harsh break detected */ |
| 38 | + GPS_EVENT_OVERACCEL, /**< Harsh/over acceleration detected */ |
| 39 | + GPS_EVENT_OVERSPEED, /**< Over-speed detected */ |
| 40 | + GPS_EVENT_SPEED_NORMAL,/**< Speed back to normal, Occurs after over-speed |
| 41 | + is detected */ |
| 42 | + GPS_EVENT_COG_CHANGED, /**< Course over ground change event. Occurs during |
| 43 | + turns or on curves */ |
| 44 | + GPS_EVENT_HARSHTURN, /**< Harsh Turning/cornering detected */ |
| 45 | + GPS_EVENT_NO_RESPONSE, /**< No response from GPS module */ |
| 46 | + GPS_EVENT_GPS_LOST, /**< GPS fix lost event */ |
| 47 | + GPS_EVENT_GPS_LOCKED, /**< GPS fix acquired event */ |
| 48 | + GPS_EVENT_GPS_OK, /**< GPS response normal, usually comes after |
| 49 | + @ref GPS_EVENT_NO_RESPONSE event when start |
| 50 | + GPS working properly again. */ |
| 51 | +}; |
| 52 | + |
| 53 | +/** |
| 54 | + * GPS Configuration parameter type |
| 55 | + */ |
| 56 | +enum gpsparam_e { |
| 57 | + GPS_PARAM_HARSHBREAK_LIMIT, /**< Set threshold for Harsh breaking in |
| 58 | + g (m/s2). Expects param as float */ |
| 59 | + GPS_PARAM_OVERACCEL_LIMIT, /**< Set threshold for Harsh acceleration in |
| 60 | + g (m/s2). Expects param as float */ |
| 61 | + GPS_PARAM_SPEEDLIMIT, /**< Set over-speed limit in Km/h, expects |
| 62 | + param as float */ |
| 63 | + GPS_PARAM_TURNINGSPEED_LIMIT,/**< Set harsh turning speed limit in Km/h, |
| 64 | + expects parameter as float */ |
| 65 | + GPS_PARAM_ODOMETER, /**< Set odometer value in meters */ |
| 66 | +}; |
| 67 | + |
| 68 | +/** |
| 69 | + * GPS point structure |
| 70 | + */ |
| 71 | +struct point_t { |
| 72 | + double lat; /**< Latitude in degrees */ |
| 73 | + double lng; /**< Longitude in degrees */ |
| 74 | +}; |
| 75 | + |
| 76 | +/** |
| 77 | + * GPS data structure |
| 78 | + */ |
| 79 | +struct gpsdata_t { |
| 80 | + char time[7]; /**< GPS UTC time as hhmmss */ |
| 81 | + char date[7]; /**< GPS UTC date as ddmmyy */ |
| 82 | + char fix; /**< GPS Fix status 'A' or 'V' */ |
| 83 | + int satinview; /**< Satellites in view and used in current fix */ |
| 84 | + float speedkmph; /**< GPS Speed in Km/h */ |
| 85 | + float direction; /**< Direction/Course over ground in degrees */ |
| 86 | + double lat; /**< Latitude in degree */ |
| 87 | + double lng; /**< Longitude in degree */ |
| 88 | + float altitude; /**< Altitude value in meters */ |
| 89 | + double odom; /**< Odometer value in meteres */ |
| 90 | + float hdop,pdop; /**< Hdop and Pdop values */ |
| 91 | +}; |
| 92 | + |
| 93 | +/** |
| 94 | + * GPS Initial configuration structure |
| 95 | + */ |
| 96 | +struct gpsconfig_t { |
| 97 | + int type; /**< GPS type @ref gpstype_e, For In-built GPS use @ref GPS_TYPE_MTK */ |
| 98 | + float speedlimit; /**< Over-speed limit in Km/h */ |
| 99 | + float harshturn; /**< Harsh Turning speed limit in Km/h */ |
| 100 | + float harshbrk; /**< Harsh breaking limit in g (m/s2) */ |
| 101 | + float overaccel; /**< Harsh acceleration limit in g (m/s2) */ |
| 102 | + /** |
| 103 | + * GPS event callback function |
| 104 | + * @param GPS Event see @ref gpsevent_e |
| 105 | + */ |
| 106 | + void (*gps_event_cb)(int); |
| 107 | + |
| 108 | + /** |
| 109 | + * GPS parsed data callback |
| 110 | + * @param GPS Data structure pointer |
| 111 | + */ |
| 112 | + void (*gps_datacallback)(struct gpsdata_t *); |
| 113 | + |
| 114 | + /** |
| 115 | + * GPS Raw data callback |
| 116 | + * @param nmea NMEA data string |
| 117 | + */ |
| 118 | + void (*gps_nmea_cb)(const char *nmea); |
| 119 | +}; |
| 120 | + |
| 121 | +/** |
| 122 | + * Initialize GPS library and start GPS data handler task |
| 123 | + * @param port [in] device file where GPS is connected, e.g. /dev/ttyS1 |
| 124 | + * @param config [in] GPS initial configuration, see @ref gpsconfig_t |
| 125 | + * @return 0 on success, negative value on failure |
| 126 | + */ |
| 127 | +int gpslib_init(char *port, struct gpsconfig_t *config); |
| 128 | + |
| 129 | +/** |
| 130 | + * Send command to GPS. GPS commands format:\n |
| 131 | + * $[command]*[checksum][cr][lf]\n |
| 132 | + * This function expect "command" as cmd parameter. The header, footer and |
| 133 | + * crc value will be calculated internally based on GPS type configured during |
| 134 | + * gps initialization see @ref gpslib_init() |
| 135 | + * @param cmd [in] GPS command without $ and CRC |
| 136 | + * @return 0 on success, negative value on failure |
| 137 | + */ |
| 138 | +int gps_sendcmd(const char *cmd); |
| 139 | + |
| 140 | +/** |
| 141 | + * Reconfigure GPS module. This function may be called if application |
| 142 | + * performs a hardware or software reset to GPS module. |
| 143 | + * @param type [in] GPS type, see @ref gpstype_e |
| 144 | + */ |
| 145 | +void gpslib_reconfig(int type); |
| 146 | + |
| 147 | +/** |
| 148 | + * Get current GPS data structure |
| 149 | + * @param gps [out] GPS structure to fill |
| 150 | + */ |
| 151 | +void gps_getdata(struct gpsdata_t *gps); |
| 152 | + |
| 153 | +/** |
| 154 | + * Get movement trace from starting position. This function can be called after |
| 155 | + * @ref GPS_EVENT_MOTION_START event has occurred. |
| 156 | + * |
| 157 | + * GPS library has complex logic to detect movement based on GPS data, which |
| 158 | + * may cause delay in firing motion start event. However GPS library keep track |
| 159 | + * of movement made from the start position to the moment when GPS event is |
| 160 | + * generated. So by calling this function the complete movement trace can be |
| 161 | + * sent to server without missing any point from track. |
| 162 | + * @param gps [out] GPS data structure to fill |
| 163 | + * @return 0 on success, -1 no more point available in trace. |
| 164 | + */ |
| 165 | +int gps_get_starttrace(struct gpsdata_t *gps); |
| 166 | + |
| 167 | +/** |
| 168 | + * Set GPS parameter |
| 169 | + * @param type [in] GPS parameter type, see @ref gpsparam_e |
| 170 | + * @param val [out] Pointer to GPS parameter value to set |
| 171 | + */ |
| 172 | +void gps_set_param(int type, void *val); |
| 173 | + |
| 174 | +/** |
| 175 | + * Get GPS parameter |
| 176 | + * @param type [in] GPS parameter type, see @ref gpsparam_e |
| 177 | + * @param val [out] GPS parameter value buffer filled on return |
| 178 | + */ |
| 179 | +void gps_get_param(int type, void *val); |
| 180 | + |
| 181 | +/** |
| 182 | + * Get GPS status |
| 183 | + * @return Returns 0 if GPS is inactive or not responding |
| 184 | + * Returns 1 if GPS is active and running fine. |
| 185 | + */ |
| 186 | +int gps_getstatus(void); |
| 187 | + |
| 188 | +/** |
| 189 | + * Write data on GPS port |
| 190 | + * @param buf [in] Buffer with data to send |
| 191 | + * @param len [in] Length of data in @a buf |
| 192 | + * @return On success, returns actual number of bytes written or -1 on failure |
| 193 | + * errno will be set on failure. |
| 194 | + */ |
| 195 | +int gps_uart_write(unsigned char *buf, int len); |
| 196 | + |
| 197 | +/** |
| 198 | + * Convert GPS string date time to seconds since Epoch. |
| 199 | + * @param date_in [in] GPS date in ddmmyy format |
| 200 | + * @param time_in [in] GPS Time in hhmmss format |
| 201 | + * @return Time value in seconds since Epoch |
| 202 | + */ |
| 203 | +time_t gpsmktime(char *date_in, char *time_in); |
| 204 | + |
| 205 | +/** |
| 206 | + * Check if point is within geofence or not |
| 207 | + * @param type [in] Geofence type, see @ref geofence_type_e |
| 208 | + * @param fence [in] For polygon type fence, Array of GPS points |
| 209 | + * For Circular fence, pointer to point |
| 210 | + * representing center of circle |
| 211 | + * @param corners_radius [in] For polygon fence, Number of corners in the fence |
| 212 | + * For circular fence, radius of fence in meters |
| 213 | + * @param point [in] Input Point/GPS coordinates to test |
| 214 | + * @return Returns True (1) if point is inside the geofence |
| 215 | + * False (0) if point is outside geofence |
| 216 | + * -22 if parameters are invalid. |
| 217 | + */ |
| 218 | +int geofence_check(int type, struct point_t *fence, unsigned int corners_radius, struct point_t *point); |
| 219 | + |
| 220 | +#endif /* INC_GPSLIB_H_ */ |
0 commit comments