aboutsummaryrefslogtreecommitdiffstats
path: root/binding
diff options
context:
space:
mode:
Diffstat (limited to 'binding')
-rw-r--r--binding/afm-gps-binding.c112
1 files changed, 97 insertions, 15 deletions
diff --git a/binding/afm-gps-binding.c b/binding/afm-gps-binding.c
index 873c264..c7b3d2f 100644
--- a/binding/afm-gps-binding.c
+++ b/binding/afm-gps-binding.c
@@ -265,18 +265,13 @@ static void add_record(json_object *jresp)
static void *data_poll(void *ptr)
{
- int tries = 0;
-
/*
* keep reading till an error condition happens
*/
- while (tries < 60) {
+ while (gps_waiting(&data, MSECS_TO_USECS(2000)) && !errno)
+ {
json_object *jresp = NULL;
- if (gps_waiting(&data, MSECS_TO_USECS(2000)) < 0) {
- tries++;
- continue;
- }
pthread_mutex_lock(&mutex);
if (gps_read(&data) == -1) {
AFB_ERROR("Cannot read from GPS daemon.\n");
@@ -372,28 +367,114 @@ static int replay_init()
return timer_settime(timer_id, 0, &ts, NULL);
}
+/**
+ * @brief setlocation request callback
+ * @param[in] request Request from server
+ */
+void set_gps(afb_req_t request)
+{
+ const char* latitude = afb_req_value(request, "Latitude");
+ const char* longitude = afb_req_value(request, "Longitude");
+
+ AFB_REQ_NOTICE(request, "latitude = %s,longitude = %s", latitude,longitude);
+
+ afb_req_success(request, NULL, NULL);
+
+ struct json_object* obj = json_object_new_object();
+
+ json_object_object_add(obj, "latitude", json_object_new_string(latitude));
+ json_object_object_add(obj, "longitude", json_object_new_string(longitude));
+
+ const char* response_json_str = json_object_to_json_string(obj);
+ AFB_REQ_NOTICE(request, "response_json_str = %s", response_json_str);
+
+ afb_event_push(location_event, obj);
+
+}
+/**
+ * @brief carlaclient binding event function
+ * @param[in] api the api serving the request
+ * @param[in] event event name
+ * @param[in] object event json object
+ */
+static void onevent(afb_api_t api, const char *event, struct json_object *object)
+{
+ AFB_API_NOTICE(api, "on_event %s , object %s", event, json_object_get_string(object));
+
+ struct json_object *lati = NULL;
+ struct json_object *longi = NULL;
+
+ if ((json_object_object_get_ex(object, "latitude", &lati)) &&
+ (json_object_object_get_ex(object, "longitude", &longi)))
+ {
+ const char* latitude = json_object_get_string(lati);
+ const char* longitude = json_object_get_string(longi);
+ struct json_object* obj = json_object_new_object();
+
+ json_object_object_add(obj, "latitude", json_object_new_string(latitude));
+ json_object_object_add(obj, "longitude", json_object_new_string(longitude));
+
+ afb_event_push(location_event, obj);
+ }
+}
+/**
+ * @brief the callback function
+ * @param[in] closure the user defined closure pointer 'closure'
+ * @param[in] a JSON object returned (can be NULL)
+ * @param[in] a string not NULL in case of error but NULL on success
+ * @param[in] a string handling some info (can be NULL)
+ * @param[in] api the api
+ */
+static void api_callback(void *closure, struct json_object *object, const char *error, const char *info, afb_api_t api)
+{
+ AFB_API_NOTICE(api, "asynchronous call, error=%s, info=%s, object=%s.", error, info, json_object_get_string(object));
+}
+
+/**
+ * @brief call api
+ * @param[in] api the api serving the request
+ * @param[in] service the api name of service
+ * @param[in] verb the verb of service
+ * @param[in] args parameter
+ */
+static void api_call(afb_api_t api, const char *service, const char *verb, struct json_object *args)
+{
+ AFB_API_NOTICE(api, "service=%s verb=%s, args=%s.", service, verb, json_object_get_string(args));
+ afb_api_call(api, service, verb, args, api_callback, 0);
+}
+/**
+ * @brief call carlaclient subscribe function
+ * @param[in] api the api serving the request
+ */
+static void SubscribeCarlaclient(afb_api_t api)
+{
+ struct json_object* obj = json_object_new_object();
+ json_object_object_add(obj, "event", json_object_new_string("positionUpdated"));
+ api_call(api, "carlaclient", "subscribe" ,obj);
+}
/*
* Test to see if in demo mode first, then enable if not enable gpsd
*/
static int init(afb_api_t api)
{
- int ret;
- location_event = afb_daemon_make_event("location");
-
- ret = replay_init();
+ // int ret;
+ location_event = afb_daemon_make_event("setlocation");
- if (!ret)
- recording.replaying = 1;
- else
- gps_init();
+ // ret = replay_init();
+ // if (!ret)
+ // recording.replaying = 1;
+ // else
+ // gps_init();
+ SubscribeCarlaclient(api);
return 0;
}
static const struct afb_verb_v3 binding_verbs[] = {
{ .verb = "location", .callback = get_data, .info = "Get GNSS data" },
+ { .verb = "setlocation", .callback = set_gps, .info = "Longitude and Latitude data of GPS" },
{ .verb = "record", .callback = record, .info = "Record GPS data" },
{ .verb = "subscribe", .callback = subscribe, .info = "Subscribe to GNSS events" },
{ .verb = "unsubscribe", .callback = unsubscribe, .info = "Unsubscribe to GNSS events" },
@@ -408,4 +489,5 @@ const struct afb_binding_v3 afbBindingV3 = {
.specification = "GNSS/GPS API",
.verbs = binding_verbs,
.init = init,
+ .onevent = onevent
};