aboutsummaryrefslogtreecommitdiffstats
path: root/uds-ble-id-init-service/src/uds-ble-id-init-service.c
blob: f88796381a461960e232c5fab67f265973d76261 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
 * Author: José Bollo <jose.bollo@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#define _GNU_SOURCE

#include <errno.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <json-c/json.h>
#include <systemd/sd-bus.h>

#define AFB_BINDING_VERSION 2
#include <afb/afb-binding.h>

#include "aia-uds-bluez.h"

#if !defined(AUTO_START_ADVISE)
#define AUTO_START_ADVISE 1
#endif

static int advising;

static struct afb_event event;

static int autoadvise = AUTO_START_ADVISE;

/****************************************************************/

static void on_uds_change(const struct aia_uds *uds)
{
	struct json_object *object;

	AFB_INFO("UDS changed"
		" first-name%s[%.*s]"
		" last-name%s[%.*s]"
		" email%s[%.*s]"
		" language%s[%.*s]",
		uds->first_name.changed ? "*" : "", (int)uds->first_name.length, uds->first_name.data ?:"",
		uds->last_name.changed ? "*" : "", (int)uds->last_name.length, uds->last_name.data ?:"",
		uds->email.changed ? "*" : "", (int)uds->email.length, uds->email.data ?:"",
		uds->language.changed ? "*" : "", (int)uds->language.length, uds->language.data ?:"");

	if (uds->email.changed) {
		object = json_object_new_object();
		json_object_object_add(object, "incoming", json_object_new_string(uds->email.data));
		afb_event_push(event, object);
	}
}

static void start (struct afb_req request)
{
	int rc;

	if (!advising) {
		rc = aia_uds_advise(1, NULL, NULL);
		if (rc < 0) {
/*
TODO: solve the issue
			afb_req_fail(request, "failed", "start scan failed");
			return;
*/
			AFB_ERROR("Ignoring scan start failed, because probably already in progress");
		}
	}
	advising = advising + 1;
	afb_req_subscribe(request, event);
	afb_req_success(request, NULL, NULL);
}


static void stop (struct afb_req request)
{
	if (advising) {
		advising = advising - 1;
		if (!advising)
			aia_uds_advise(0, NULL, NULL);
	}
	afb_req_success(request, NULL, NULL);
}

static int init()
{
	sd_bus *bus;
	int rc;

	bus = afb_daemon_get_system_bus();
	rc = bus ? aia_uds_init(bus) : -ENOTSUP;	
	if (rc < 0) {
		errno = -rc;
		return -1;
	}

	aia_uds_set_on_change(on_uds_change);

	event = afb_daemon_make_event("event");
	if (!afb_event_is_valid(event))
		return -1;

	rc = aia_uds_advise(autoadvise, NULL, NULL);
	advising = autoadvise && rc >= 0;
	return rc < 0 ? rc : 0;
}


// NOTE: this sample does not use session to keep test a basic as possible
//     in real application most APIs should be protected with AFB_SESSION_CHECK
static const struct afb_verb_v2 verbs[]=
{
  {"start"  , start, NULL, "start User Data Service", AFB_SESSION_NONE },
  {"stop"   , stop , NULL, "stop User Data Service" , AFB_SESSION_NONE },
  {NULL}
};

const struct afb_binding_v2 afbBindingV2 =
{
	.api = "uds-ble-init-id",
	.specification = NULL,
	.info = "AGL Identitity initiator above BLE's User Data Service",
	.verbs = verbs,
	.preinit = NULL,
	.init = init,
	.onevent = NULL,
	.noconcurrency = 1
};

/* vim: set colorcolumn=80: */