aboutsummaryrefslogtreecommitdiffstats
path: root/binding/network-api.h
blob: e01216b710614fa0dc5d8e34b12e815d788e6807 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * Copyright 2018 Konsulko Group
 * Author: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
 *
 * 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.
 */

#ifndef NETWORK_API_H
#define NETWORK_API_H

#include <alloca.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>

#include <glib.h>

#include <json-c/json.h>

#define CONNMAN_SERVICE                 	"net.connman"
#define CONNMAN_MANAGER_INTERFACE		CONNMAN_SERVICE ".Manager"
#define CONNMAN_TECHNOLOGY_INTERFACE		CONNMAN_SERVICE ".Technology"
#define CONNMAN_SERVICE_INTERFACE		CONNMAN_SERVICE ".Service"
#define CONNMAN_PROFILE_INTERFACE		CONNMAN_SERVICE ".Profile"
#define CONNMAN_COUNTER_INTERFACE		CONNMAN_SERVICE ".Counter"
#define CONNMAN_ERROR_INTERFACE			CONNMAN_SERVICE ".Error"
#define CONNMAN_AGENT_INTERFACE			CONNMAN_SERVICE ".Agent"

#define CONNMAN_MANAGER_PATH			"/"
#define CONNMAN_PATH				"/net/connman"
#define CONNMAN_TECHNOLOGY_PREFIX		CONNMAN_PATH "/technology"
#define CONNMAN_SERVICE_PREFIX			CONNMAN_PATH "/service"

#define CONNMAN_TECHNOLOGY_PATH(_t) \
    ({ \
     const char *__t = (_t); \
     size_t __len = strlen(CONNMAN_TECHNOLOGY_PREFIX) + 1 + \
     strlen(__t) + 1; \
     char *__tpath; \
     __tpath = alloca(__len + 1 + 1); \
     snprintf(__tpath, __len + 1, \
             CONNMAN_TECHNOLOGY_PREFIX "/%s", __t); \
             __tpath; \
     })

#define CONNMAN_SERVICE_PATH(_s) \
    ({ \
     const char *__s = (_s); \
     size_t __len = strlen(CONNMAN_SERVICE_PREFIX) + 1 + \
     strlen(__s) + 1; \
     char *__spath; \
     __spath = alloca(__len + 1 + 1); \
     snprintf(__spath, __len + 1, \
             CONNMAN_SERVICE_PREFIX "/%s", __s); \
             __spath; \
     })

#define AGENT_PATH				"/net/connman/Agent"
#define AGENT_SERVICE				"org.agent"

#define DBUS_REPLY_TIMEOUT			(120 * 1000)
#define DBUS_REPLY_TIMEOUT_SHORT		(10 * 1000)

#define CONNMAN_AT_MANAGER			"manager"
#define CONNMAN_AT_TECHNOLOGY			"technology"
#define CONNMAN_AT_SERVICE			"service"

struct network_state;

static inline const char *connman_strip_path(const char *path)
{
	const char *basename;

	basename = strrchr(path, '/');
	if (!basename)
		return NULL;
	basename++;
	/* at least one character */
	return *basename ? basename : NULL;
}

const struct property_info *connman_get_property_info(
		const char *access_type, GError **error);

gboolean connman_property_dbus2json(const char *access_type,
		json_object *jprop, const gchar *key, GVariant *var,
		gboolean *is_config,
		GError **error);

GVariant *connman_call(struct network_state *ns,
		const char *access_type, const char *type_arg,
		const char *method, GVariant *params, GError **error);

json_object *connman_get_properties(struct network_state *ns,
		const char *access_type, const char *type_arg,
		GError **error);

json_object *connman_get_property(struct network_state *ns,
		const char *access_type, const char *type_arg,
		gboolean is_json_name, const char *name, GError **error);

gboolean connman_set_property(struct network_state *ns,
		const char *access_type, const char *type_arg,
		gboolean is_json_name, const char *name, json_object *jval,
		GError **error);

/* convenience access methods */
static inline gboolean manager_property_dbus2json(json_object *jprop,
		const gchar *key, GVariant *var, gboolean *is_config,
		GError **error)
{
	return connman_property_dbus2json(CONNMAN_AT_MANAGER,
			jprop, key, var, is_config, error);
}

static inline gboolean technology_property_dbus2json(json_object *jprop,
		const gchar *key, GVariant *var, gboolean *is_config,
		GError **error)
{
	return connman_property_dbus2json(CONNMAN_AT_TECHNOLOGY,
			jprop, key, var, is_config, error);
}

static inline gboolean service_property_dbus2json(json_object *jprop,
		const gchar *key, GVariant *var, gboolean *is_config,
		GError **error)
{
	return connman_property_dbus2json(CONNMAN_AT_SERVICE,
			jprop, key, var, is_config, error);
}

static inline GVariant *manager_call(struct network_state *ns,
		const char *method, GVariant *params, GError **error)
{
	return connman_call(ns, CONNMAN_AT_MANAGER, NULL,
			method, params, error);
}

static inline GVariant *technology_call(struct network_state *ns,
		const char *technology, const char *method,
		GVariant *params, GError **error)
{
	return connman_call(ns, CONNMAN_AT_TECHNOLOGY, technology,
			method, params, error);
}

static inline GVariant *service_call(struct network_state *ns,
		const char *service, const char *method,
		GVariant *params, GError **error)
{
	return connman_call(ns, CONNMAN_AT_SERVICE, service,
			method, params, error);
}

static inline json_object *manager_properties(struct network_state *ns, GError **error)
{
	return connman_get_properties(ns,
			CONNMAN_AT_MANAGER, NULL, error);
}

static inline json_object *technology_properties(struct network_state *ns,
		GError **error, const gchar *technology)
{
	return connman_get_properties(ns,
			CONNMAN_AT_TECHNOLOGY, technology, error);
}

static inline json_object *service_properties(struct network_state *ns,
		GError **error, const gchar *service)
{
	return connman_get_properties(ns,
			CONNMAN_AT_SERVICE, service, error);
}

static inline json_object *manager_get_property(struct network_state *ns,
		gboolean is_json_name, const char *name, GError **error)
{
	return connman_get_property(ns, CONNMAN_AT_MANAGER, NULL,
			is_json_name, name, error);
}

static inline json_object *technology_get_property(struct network_state *ns,
		const char *technology,
		gboolean is_json_name, const char *name, GError **error)
{
	return connman_get_property(ns, CONNMAN_AT_TECHNOLOGY, technology,
			is_json_name, name, error);
}

static inline json_object *service_get_property(struct network_state *ns,
		const char *service,
		gboolean is_json_name, const char *name, GError **error)
{
	return connman_get_property(ns, CONNMAN_AT_SERVICE, service,
			is_json_name, name, error);
}

static inline gboolean manager_set_property(struct network_state *ns,
		gboolean is_json_name, const char *name, json_object *jval,
		GError **error)
{
	return connman_set_property(ns, CONNMAN_AT_MANAGER, NULL,
				    is_json_name, name, jval, error);

}

static inline gboolean technology_set_property(struct network_state *ns,
		const char *technology,
		gboolean is_json_name, const char *name, json_object *jval,
		GError **error)
{
	return connman_set_property(ns, CONNMAN_AT_TECHNOLOGY, technology,
				    is_json_name, name, jval, error);
}

static inline gboolean service_set_property(struct network_state *ns,
		const char *service,
		gboolean is_json_name, const char *name, json_object *jval,
		GError **error)
{
	return connman_set_property(ns, CONNMAN_AT_SERVICE, service,
				    is_json_name, name, jval, error);
}

struct connman_pending_work {
	struct network_state *ns;
	void *user_data;
	GCancellable *cancel;
	void (*callback)(void *user_data, GVariant *result, GError **error);
};

void connman_cancel_call(struct network_state *ns,
		struct connman_pending_work *cpw);

struct connman_pending_work *
connman_call_async(struct network_state *ns,
		const char *access_type, const char *type_arg,
		const char *method, GVariant *params, GError **error,
		void (*callback)(void *user_data, GVariant *result, GError **error),
		void *user_data);

void connman_decode_call_error(struct network_state *ns,
		const char *access_type, const char *type_arg,
		const char *method,
		GError **error);

#endif /* NETWORK_API_H */