Skip to content

Commit 4e7387a

Browse files
author
Lukasz Baj
committed
[SRC] Handle GPS position
1 parent be0d741 commit 4e7387a

File tree

4 files changed

+115
-72
lines changed

4 files changed

+115
-72
lines changed

metadata_input_netlink.c

Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -204,73 +204,13 @@ static void md_input_netlink_handle_conn_event(struct md_input_netlink *min,
204204
static void md_input_netlink_handle_gps_event(struct md_input_netlink *min,
205205
struct json_object *json_obj)
206206
{
207-
struct md_gps_event gps_event;
208-
uint8_t retval = 0;
209-
int8_t sentence_id = 0;
210-
211-
union {
212-
struct minmea_sentence_gga gga;
213-
struct minmea_sentence_rmc rmc;
214-
} gps;
215-
216-
memset(&gps_event, 0, sizeof(struct md_gps_event));
217-
gps_event.md_type = META_TYPE_POS;
218-
219-
json_object_object_foreach(json_obj, key, val) {
220-
if (!strcmp(key, "md_seq"))
221-
gps_event.sequence = (uint16_t) json_object_get_int(val);
222-
223-
if (!strcmp(key, "timestamp"))
224-
gps_event.tstamp_tv.tv_sec = json_object_get_int64(val);
225-
226-
if (!strcmp(key, "nmea_string"))
227-
gps_event.nmea_raw = json_object_get_string(val);
228-
}
229-
230-
if (!gps_event.sequence || !gps_event.nmea_raw)
231-
return;
232-
233-
sentence_id = minmea_sentence_id(gps_event.nmea_raw, 0);
234-
235-
if (sentence_id <= 0)
236-
return;
207+
struct md_gps_event* event = handle_gps_event(json_obj);
237208

238-
gps_event.minmea_id = sentence_id;
239-
240-
//We can ignore NMEA checksum
241-
switch (sentence_id) {
242-
case MINMEA_SENTENCE_GGA:
243-
retval = minmea_parse_gga(&gps.gga, gps_event.nmea_raw);
244-
if (retval && !gps.gga.fix_quality) {
245-
retval = 0;
246-
} else {
247-
gps_event.time = gps.gga.time;
248-
gps_event.latitude = minmea_tocoord(&gps.gga.latitude);
249-
gps_event.longitude = minmea_tocoord(&gps.gga.longitude);
250-
gps_event.altitude = minmea_tofloat(&gps.gga.altitude);
251-
gps_event.satellites_tracked = gps.gga.satellites_tracked;
252-
}
253-
break;
254-
case MINMEA_SENTENCE_RMC:
255-
retval = minmea_parse_rmc(&gps.rmc, gps_event.nmea_raw);
256-
257-
if (retval && !gps.rmc.valid) {
258-
retval = 0;
259-
} else {
260-
gps_event.time = gps.rmc.time;
261-
gps_event.latitude = minmea_tocoord(&gps.rmc.latitude);
262-
gps_event.longitude = minmea_tocoord(&gps.rmc.longitude);
263-
gps_event.speed = minmea_tofloat(&gps.rmc.speed);
264-
}
265-
break;
266-
default:
267-
break;
268-
}
269-
270-
if (!retval)
209+
if (!event)
271210
return;
272211

273-
mde_publish_event_obj(min->parent, (struct md_event *) &gps_event);
212+
mde_publish_event_obj(min->parent, (struct md_event *) &event);
213+
free(event);
274214
}
275215

276216
static void md_input_netlink_handle_system_event(struct md_input_netlink *min,

metadata_input_nl_zmq_common.c

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ uint8_t parse_conn_info(struct json_object *meta_obj, struct md_conn_event *mce,
5959

6060
if (mce->event_param == CONN_EVENT_DATA_USAGE_UPDATE) {
6161
if (!mce->tstamp || !mce->event_param || !mce->interface_id || (mce->imei && !mce->imsi) ||
62-
(mce->imsi && !mce->imei)) {
62+
(mce->imsi && !mce->imei)) {
6363
META_PRINT_SYSLOG(parent, LOG_ERR, "Missing required argument in usage JSON\n");
6464
return RETVAL_FAILURE;
6565
} else {
@@ -68,10 +68,10 @@ uint8_t parse_conn_info(struct json_object *meta_obj, struct md_conn_event *mce,
6868
}
6969

7070
if (!mce->tstamp || !mce->sequence ||
71-
!mce->l3_session_id || !mce->event_param ||
72-
!mce->interface_type || !mce->network_address_family ||
73-
!mce->network_address || !mce->interface_id ||
74-
!mce->interface_id_type) {
71+
!mce->l3_session_id || !mce->event_param ||
72+
!mce->interface_type || !mce->network_address_family ||
73+
!mce->network_address || !mce->interface_id ||
74+
!mce->interface_id_type) {
7575
META_PRINT_SYSLOG(parent, LOG_ERR, "Missing required argument in JSON\n");
7676
return RETVAL_FAILURE;
7777
}
@@ -448,8 +448,85 @@ struct md_radio_wcdma_cell_id_event* radio_wcdma_cell_id(json_object *obj)
448448
return event;
449449
}
450450

451+
struct md_gps_event* handle_gps_event(struct json_object *json_obj)
452+
{
453+
int8_t sentence_id = 0;
454+
455+
struct md_gps_event *gps_event = calloc(sizeof(struct md_gps_event), 1);
456+
457+
if (!gps_event)
458+
return NULL;
459+
460+
union {
461+
struct minmea_sentence_gga gga;
462+
struct minmea_sentence_rmc rmc;
463+
} gps;
464+
465+
gps_event->md_type = META_TYPE_POS;
466+
467+
json_object_object_foreach(json_obj, key, val) {
468+
if (!strcmp(key, "md_seq"))
469+
gps_event->sequence = (uint16_t) json_object_get_int(val);
470+
471+
if (!strcmp(key, "timestamp"))
472+
gps_event->tstamp_tv.tv_sec = json_object_get_int64(val);
473+
474+
if (!strcmp(key, "nmea_string"))
475+
gps_event->nmea_raw = json_object_get_string(val);
476+
}
477+
478+
if (!gps_event->sequence || !gps_event->nmea_raw)
479+
{
480+
free(gps_event);
481+
return NULL;
482+
}
483+
484+
sentence_id = minmea_sentence_id(gps_event->nmea_raw, 0);
485+
486+
if (sentence_id <= 0)
487+
{
488+
free(gps_event);
489+
return NULL;
490+
}
491+
492+
gps_event->minmea_id = sentence_id;
493+
494+
//We can ignore NMEA checksum
495+
switch (sentence_id) {
496+
case MINMEA_SENTENCE_GGA:
497+
if (minmea_parse_gga(&gps.gga, gps_event->nmea_raw) && !gps.gga.fix_quality) {
498+
free(gps_event);
499+
return NULL;
500+
} else {
501+
gps_event->time = gps.gga.time;
502+
gps_event->latitude = minmea_tocoord(&gps.gga.latitude);
503+
gps_event->longitude = minmea_tocoord(&gps.gga.longitude);
504+
gps_event->altitude = minmea_tofloat(&gps.gga.altitude);
505+
gps_event->satellites_tracked = gps.gga.satellites_tracked;
506+
}
507+
break;
508+
case MINMEA_SENTENCE_RMC:
509+
if (minmea_parse_rmc(&gps.rmc, gps_event->nmea_raw) && !gps.rmc.valid) {
510+
free(gps_event);
511+
return NULL;
512+
} else {
513+
gps_event->time = gps.rmc.time;
514+
gps_event->latitude = minmea_tocoord(&gps.rmc.latitude);
515+
gps_event->longitude = minmea_tocoord(&gps.rmc.longitude);
516+
gps_event->speed = minmea_tofloat(&gps.rmc.speed);
517+
}
518+
break;
519+
default:
520+
free(gps_event);
521+
return NULL;
522+
break;
523+
}
524+
525+
return gps_event;
526+
}
527+
451528
uint8_t add_json_key_value(const char *key,
452-
int32_t value, struct json_object *obj)
529+
int32_t value, struct json_object *obj)
453530
{
454531
struct json_object *obj_add = NULL;
455532
obj_add = json_object_new_int(value);

metadata_input_nl_zmq_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct md_radio_gsm_rr_cipher_mode_event* radio_gsm_rr_cipher_mode(json_object *
1212
struct md_radio_gsm_rr_channel_conf_event* radio_gsm_rr_channel_conf(json_object *obj);
1313
struct md_radio_wcdma_rrc_state_event* radio_wcdma_rrc_state(json_object *obj);
1414
struct md_radio_wcdma_cell_id_event* radio_wcdma_cell_id(json_object *obj);
15+
struct md_gps_event* handle_gps_event(struct json_object *json_obj);
1516
uint8_t add_json_key_value(const char *key, int32_t value, struct json_object *obj);
1617
void init_iface_event(struct md_iface_event *mie);
1718
void init_conn_event(struct md_conn_event *mce);

metadata_input_zeromq.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,31 @@ static void md_input_zeromq_handle_radio_event(struct md_input_zeromq *miz,
201201
}
202202
}
203203

204+
static void md_input_zeromq_handle_gps_event(struct md_input_zeromq *miz,
205+
struct json_object *json_obj)
206+
{
207+
struct md_gps_event* event = handle_gps_event(json_obj);
208+
209+
if (!event)
210+
return;
211+
212+
mde_publish_event_obj(miz->parent, (struct md_event *) &event);
213+
free(event);
214+
}
215+
216+
static void md_input_zeromq_handle_system_event(struct md_input_zeromq *miz,
217+
struct json_object *obj)
218+
{
219+
//recycle iface event, it contains all fields we need (currently)
220+
memset(miz->mse, 0, sizeof(md_system_event_t));
221+
miz->mse->md_type = META_TYPE_SYSTEM;
222+
223+
if (parse_iface_event(obj, miz->mse, miz->parent) == RETVAL_FAILURE)
224+
return;
225+
226+
mde_publish_event_obj(miz->parent, (struct md_event*) miz->mse);
227+
}
228+
204229
static int subscribe_for_topic(const char* topic, struct md_input_zeromq *miz)
205230
{
206231
size_t len = strlen(topic);
@@ -274,13 +299,13 @@ static void md_input_zeromq_handle_event(void *ptr, int32_t fd, uint32_t events)
274299
md_input_zeromq_handle_conn_event(miz, zmqh_obj);
275300
break;
276301
case META_TYPE_POS:
277-
//md_input_netlink_handle_gps_event(miz, zmqh_obj);
302+
md_input_zeromq_handle_gps_event(miz, zmqh_obj);
278303
break;
279304
case META_TYPE_RADIO:
280305
md_input_zeromq_handle_radio_event(miz, zmqh_obj);
281306
break;
282307
case META_TYPE_SYSTEM:
283-
//md_input_netlink_handle_system_event(miz, zmqh_obj);
308+
md_input_zeromq_handle_system_event(miz, zmqh_obj);
284309
break;
285310
default:
286311
META_PRINT(miz->parent->logfile, "Unknown event type\n");

0 commit comments

Comments
 (0)