Asterisk - The Open Source Telephony Project GIT-master-abe0018
Data Structures | Macros | Functions | Variables
geoloc_datastore.c File Reference
#include "asterisk.h"
#include "asterisk/astobj2.h"
#include "asterisk/datastore.h"
#include "asterisk/channel.h"
#include "asterisk/res_geolocation.h"
#include "asterisk/vector.h"
#include "geoloc_private.h"
Include dependency graph for geoloc_datastore.c:

Go to the source code of this file.

Data Structures

struct  eprofiles_datastore
 
struct  eprofiles_datastore::geoloc_eprofiles
 

Macros

#define GEOLOC_DS_TYPE   "geoloc_eprofiles"
 
#define IS_GEOLOC_DS(_ds)   (_ds && _ds->data && ast_strings_equal(_ds->info->type, GEOLOC_DS_TYPE))
 

Functions

int ast_geoloc_datastore_add_eprofile (struct ast_datastore *ds, struct ast_geoloc_eprofile *eprofile)
 Add an eprofile to a datastore. More...
 
struct ast_datastoreast_geoloc_datastore_create (const char *id)
 Create an empty geoloc datastore. More...
 
struct ast_datastoreast_geoloc_datastore_create_from_eprofile (struct ast_geoloc_eprofile *eprofile)
 Create a geoloc datastore from an effective profile. More...
 
struct ast_datastoreast_geoloc_datastore_create_from_profile_name (const char *profile_name)
 Geolocation datastore Functions. More...
 
int ast_geoloc_datastore_delete_eprofile (struct ast_datastore *ds, int ix)
 Delete a specific eprofile from a datastore by index. More...
 
struct ast_datastoreast_geoloc_datastore_find (struct ast_channel *chan)
 Retrieves the geoloc datastore from a channel, if any. More...
 
struct ast_geoloc_eprofileast_geoloc_datastore_get_eprofile (struct ast_datastore *ds, int ix)
 Retrieve a specific eprofile from a datastore by index. More...
 
const char * ast_geoloc_datastore_get_id (struct ast_datastore *ds)
 Retrieve a geoloc datastore's id. More...
 
int ast_geoloc_datastore_insert_eprofile (struct ast_datastore *ds, struct ast_geoloc_eprofile *eprofile, int index)
 Insert an eprofile to a datastore at the specified position. More...
 
int ast_geoloc_datastore_set_inheritance (struct ast_datastore *ds, int inherit)
 Sets the inheritance flag on the datastore. More...
 
int ast_geoloc_datastore_size (struct ast_datastore *ds)
 Retrieves the number of eprofiles in the datastore. More...
 
int geoloc_channel_load (void)
 
int geoloc_channel_reload (void)
 
int geoloc_channel_unload (void)
 
static void * geoloc_datastore_duplicate (void *obj)
 
static void geoloc_datastore_free (void *obj)
 

Variables

static const struct ast_datastore_info geoloc_datastore_info
 
struct ast_sorcerygeoloc_sorcery
 

Macro Definition Documentation

◆ GEOLOC_DS_TYPE

#define GEOLOC_DS_TYPE   "geoloc_eprofiles"

Definition at line 27 of file geoloc_datastore.c.

◆ IS_GEOLOC_DS

#define IS_GEOLOC_DS (   _ds)    (_ds && _ds->data && ast_strings_equal(_ds->info->type, GEOLOC_DS_TYPE))

Definition at line 84 of file geoloc_datastore.c.

Function Documentation

◆ ast_geoloc_datastore_add_eprofile()

int ast_geoloc_datastore_add_eprofile ( struct ast_datastore ds,
struct ast_geoloc_eprofile eprofile 
)

Add an eprofile to a datastore.

Parameters
dsThe datastore
eprofileThe eprofile to add.
Returns
The new number of eprofiles or -1 to indicate a failure.

Definition at line 135 of file geoloc_datastore.c.

137{
138 struct eprofiles_datastore *eds = NULL;
139 int rc = 0;
140
141 if (!IS_GEOLOC_DS(ds) || !eprofile) {
142 return -1;
143 }
144
145 eds = ds->data;
146 rc = AST_VECTOR_APPEND(&eds->eprofiles, ao2_bump(eprofile));
147 if (rc != 0) {
148 ao2_ref(eprofile, -1);
149 ast_log(LOG_ERROR, "Couldn't add eprofile '%s' to geoloc datastore '%s'\n", eprofile->id, eds->id);
150 return -1;
151 }
152
153 return AST_VECTOR_SIZE(&eds->eprofiles);
154}
#define ast_log
Definition: astobj2.c:42
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
#define IS_GEOLOC_DS(_ds)
#define LOG_ERROR
#define NULL
Definition: resample.c:96
void * data
Definition: datastore.h:66
const ast_string_field id
struct eprofiles_datastore::geoloc_eprofiles eprofiles
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:609
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256

References ao2_bump, ao2_ref, ast_log, AST_VECTOR_APPEND, AST_VECTOR_SIZE, ast_datastore::data, eprofiles_datastore::eprofiles, ast_geoloc_eprofile::id, eprofiles_datastore::id, IS_GEOLOC_DS, LOG_ERROR, and NULL.

Referenced by add_eprofile_to_channel(), ast_geoloc_datastore_create_from_eprofile(), ast_geoloc_datastore_create_from_profile_name(), and geoloc_profile_write().

◆ ast_geoloc_datastore_create()

struct ast_datastore * ast_geoloc_datastore_create ( const char *  id)

Create an empty geoloc datastore.

Parameters
idAn id to use for the datastore.
Returns
The datastore.

Definition at line 99 of file geoloc_datastore.c.

100{
101 struct ast_datastore *ds = NULL;
102 struct eprofiles_datastore *eds = NULL;
103 int rc = 0;
104
105 if (ast_strlen_zero(id)) {
106 ast_log(LOG_ERROR, "A geoloc datastore can't be allocated with a NULL or empty id\n");
107 return NULL;
108 }
109
111 if (!ds) {
112 ast_log(LOG_ERROR, "Geoloc datastore '%s' couldn't be allocated\n", id);
113 return NULL;
114 }
115
116 eds = ast_calloc(1, sizeof(*eds));
117 if (!eds) {
119 ast_log(LOG_ERROR, "Private structure for geoloc datastore '%s' couldn't be allocated\n", id);
120 return NULL;
121 }
122 ds->data = eds;
123
124
125 rc = AST_VECTOR_INIT(&eds->eprofiles, 2);
126 if (rc != 0) {
128 ast_log(LOG_ERROR, "Vector for geoloc datastore '%s' couldn't be initialized\n", id);
129 return NULL;
130 }
131
132 return ds;
133}
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:85
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
static const struct ast_datastore_info geoloc_datastore_info
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Structure for a data store object.
Definition: datastore.h:64
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition: vector.h:113

References ast_calloc, ast_datastore_alloc, ast_datastore_free(), ast_log, ast_strlen_zero(), AST_VECTOR_INIT, ast_datastore::data, eprofiles_datastore::eprofiles, geoloc_datastore_info, LOG_ERROR, and NULL.

Referenced by add_eprofile_to_channel(), ast_geoloc_datastore_create_from_eprofile(), ast_geoloc_datastore_create_from_profile_name(), and geoloc_profile_write().

◆ ast_geoloc_datastore_create_from_eprofile()

struct ast_datastore * ast_geoloc_datastore_create_from_eprofile ( struct ast_geoloc_eprofile eprofile)

Create a geoloc datastore from an effective profile.

Parameters
eprofileThe effective profile to use.
Returns
The datastore.

Definition at line 242 of file geoloc_datastore.c.

244{
245 struct ast_datastore *ds;
246 int rc = 0;
247
248 if (!eprofile) {
249 return NULL;
250 }
251
252 ds = ast_geoloc_datastore_create(eprofile->id);
253 if (!ds) {
254 return NULL;
255 }
256
257 rc = ast_geoloc_datastore_add_eprofile(ds, eprofile);
258 if (rc <= 0) {
260 ds = NULL;
261 }
262
263 return ds;
264}
struct ast_datastore * ast_geoloc_datastore_create(const char *id)
Create an empty geoloc datastore.
int ast_geoloc_datastore_add_eprofile(struct ast_datastore *ds, struct ast_geoloc_eprofile *eprofile)
Add an eprofile to a datastore.

References ast_datastore_free(), ast_geoloc_datastore_add_eprofile(), ast_geoloc_datastore_create(), ast_geoloc_eprofile::id, and NULL.

◆ ast_geoloc_datastore_create_from_profile_name()

struct ast_datastore * ast_geoloc_datastore_create_from_profile_name ( const char *  profile_name)

Geolocation datastore Functions.

Create a geoloc datastore from a profile name

Parameters
profile_nameThe name of the profile to use.
Returns
The datastore.

Definition at line 266 of file geoloc_datastore.c.

267{
268 struct ast_datastore *ds = NULL;
269 struct ast_geoloc_eprofile *eprofile = NULL;
270 struct ast_geoloc_profile *profile = NULL;
271 int rc = 0;
272
273 if (ast_strlen_zero(profile_name)) {
274 return NULL;
275 }
276
277 profile = ast_sorcery_retrieve_by_id(geoloc_sorcery, "profile", profile_name);
278 if (!profile) {
279 ast_log(LOG_ERROR, "A profile with the name '%s' was not found\n", profile_name);
280 return NULL;
281 }
282
283 ds = ast_geoloc_datastore_create(profile_name);
284 if (!ds) {
285 ast_log(LOG_ERROR, "A datastore couldn't be allocated for profile '%s'\n", profile_name);
286 ao2_ref(profile, -1);
287 return NULL;
288 }
289
290 eprofile = ast_geoloc_eprofile_create_from_profile(profile);
291 ao2_ref(profile, -1);
292 if (!eprofile) {
294 ast_log(LOG_ERROR, "An effective profile with the name '%s' couldn't be allocated\n", profile_name);
295 return NULL;
296 }
297
298 rc = ast_geoloc_datastore_add_eprofile(ds, eprofile);
299 ao2_ref(eprofile, -1);
300 if (rc <= 0) {
302 ds = NULL;
303 }
304
305 return ds;
306}
struct ast_sorcery * geoloc_sorcery
struct ast_geoloc_eprofile * ast_geoloc_eprofile_create_from_profile(struct ast_geoloc_profile *profile)
Allocate a new effective profile from an existing profile.
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853

References ao2_ref, ast_datastore_free(), ast_geoloc_datastore_add_eprofile(), ast_geoloc_datastore_create(), ast_geoloc_eprofile_create_from_profile(), ast_log, ast_sorcery_retrieve_by_id(), ast_strlen_zero(), geoloc_sorcery, LOG_ERROR, and NULL.

◆ ast_geoloc_datastore_delete_eprofile()

int ast_geoloc_datastore_delete_eprofile ( struct ast_datastore ds,
int  ix 
)

Delete a specific eprofile from a datastore by index.

Parameters
dsThe datastore
ixThe index
Returns
0 if succesful, -1 otherwise.

Definition at line 224 of file geoloc_datastore.c.

225{
226 struct eprofiles_datastore *eds = NULL;
227
228 if (!IS_GEOLOC_DS(ds)) {
229 return -1;
230 }
231
232 eds = ds->data;
233
234 if (ix >= AST_VECTOR_SIZE(&eds->eprofiles)) {
235 return -1;
236 }
237
238 ao2_ref(AST_VECTOR_REMOVE(&eds->eprofiles, ix, 1), -1);
239 return 0;
240}
#define AST_VECTOR_REMOVE(vec, idx, preserve_ordered)
Remove an element from a vector by index.
Definition: vector.h:412

References ao2_ref, AST_VECTOR_REMOVE, AST_VECTOR_SIZE, ast_datastore::data, eprofiles_datastore::eprofiles, IS_GEOLOC_DS, and NULL.

◆ ast_geoloc_datastore_find()

struct ast_datastore * ast_geoloc_datastore_find ( struct ast_channel chan)

Retrieves the geoloc datastore from a channel, if any.

Parameters
chanChannel
Returns
datastore if found, NULL otherwise.

Definition at line 219 of file geoloc_datastore.c.

220{
222}
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2399

References ast_channel_datastore_find(), geoloc_datastore_info, and NULL.

Referenced by geoloc_profile_read(), geoloc_profile_write(), and handle_outgoing_request().

◆ ast_geoloc_datastore_get_eprofile()

struct ast_geoloc_eprofile * ast_geoloc_datastore_get_eprofile ( struct ast_datastore ds,
int  ix 
)

Retrieve a specific eprofile from a datastore by index.

Parameters
dsThe datastore
ixThe index
Returns
The effective profile ao2 object with its reference count bumped.

Definition at line 200 of file geoloc_datastore.c.

201{
202 struct eprofiles_datastore *eds = NULL;
203 struct ast_geoloc_eprofile *eprofile;
204
205 if (!IS_GEOLOC_DS(ds)) {
206 return NULL;
207 }
208
209 eds = ds->data;
210
211 if (ix >= AST_VECTOR_SIZE(&eds->eprofiles)) {
212 return NULL;
213 }
214
215 eprofile = AST_VECTOR_GET(&eds->eprofiles, ix);
216 return ao2_bump(eprofile);
217}
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:680

References ao2_bump, AST_VECTOR_GET, AST_VECTOR_SIZE, ast_datastore::data, eprofiles_datastore::eprofiles, IS_GEOLOC_DS, and NULL.

Referenced by ast_geoloc_eprofiles_to_pidf(), geoloc_profile_read(), geoloc_profile_write(), and handle_outgoing_request().

◆ ast_geoloc_datastore_get_id()

const char * ast_geoloc_datastore_get_id ( struct ast_datastore ds)

Retrieve a geoloc datastore's id.

Parameters
dsThe datastore
Returns
The datastore's id.

Definition at line 86 of file geoloc_datastore.c.

87{
88 struct eprofiles_datastore *eds = NULL;
89
90 if (!IS_GEOLOC_DS(ds)) {
91 return NULL;
92 }
93
94 eds = (struct eprofiles_datastore *)ds->data;
95
96 return eds->id;
97}

References ast_datastore::data, eprofiles_datastore::id, IS_GEOLOC_DS, and NULL.

◆ ast_geoloc_datastore_insert_eprofile()

int ast_geoloc_datastore_insert_eprofile ( struct ast_datastore ds,
struct ast_geoloc_eprofile eprofile,
int  index 
)

Insert an eprofile to a datastore at the specified position.

Parameters
dsThe datastore
eprofileThe eprofile to add.
indexThe position to insert at. Existing eprofiles will be moved up to make room.
Returns
The new number of eprofiles or -1 to indicate a failure.

Definition at line 156 of file geoloc_datastore.c.

158{
159 struct eprofiles_datastore *eds = NULL;
160 int rc = 0;
161
162 if (!IS_GEOLOC_DS(ds) || !eprofile) {
163 return -1;
164 }
165
166 eds = ds->data;
167 rc = AST_VECTOR_INSERT_AT(&eds->eprofiles, index, ao2_bump(eprofile));
168 if (rc != 0) {
169 ao2_ref(eprofile, -1);
170 ast_log(LOG_ERROR, "Couldn't add eprofile '%s' to geoloc datastore '%s' in position '%d'\n",
171 eprofile->id, eds->id, index);
172 return -1;
173 }
174
175 return AST_VECTOR_SIZE(&eds->eprofiles);
176}
#define AST_VECTOR_INSERT_AT(vec, idx, elem)
Insert an element at a specific position in a vector, growing the vector if needed.
Definition: vector.h:338

References ao2_bump, ao2_ref, ast_log, AST_VECTOR_INSERT_AT, AST_VECTOR_SIZE, ast_datastore::data, eprofiles_datastore::eprofiles, ast_geoloc_eprofile::id, eprofiles_datastore::id, IS_GEOLOC_DS, LOG_ERROR, and NULL.

◆ ast_geoloc_datastore_set_inheritance()

int ast_geoloc_datastore_set_inheritance ( struct ast_datastore ds,
int  inherit 
)

Sets the inheritance flag on the datastore.

Parameters
dsThe datastore
inherit1 to allow the datastore to be inherited by other channels 0 to prevent the datastore to be inherited by other channels
Returns
0 if successful, -1 otherwise.

Definition at line 191 of file geoloc_datastore.c.

192{
193 if (!IS_GEOLOC_DS(ds)) {
194 return -1;
195 }
196 ds->inheritance = inherit ? DATASTORE_INHERIT_FOREVER : 0;
197 return 0;
198}
#define DATASTORE_INHERIT_FOREVER
Definition: channel.h:192
unsigned int inheritance
Definition: datastore.h:69

References DATASTORE_INHERIT_FOREVER, ast_datastore::inheritance, and IS_GEOLOC_DS.

Referenced by add_eprofile_to_channel(), and geoloc_profile_write().

◆ ast_geoloc_datastore_size()

int ast_geoloc_datastore_size ( struct ast_datastore ds)

Retrieves the number of eprofiles in the datastore.

Parameters
dsThe datastore
Returns
The number of eprofiles.

Definition at line 178 of file geoloc_datastore.c.

179{
180 struct eprofiles_datastore *eds = NULL;
181
182 if (!IS_GEOLOC_DS(ds)) {
183 return -1;
184 }
185
186 eds = ds->data;
187
188 return AST_VECTOR_SIZE(&eds->eprofiles);
189}

References AST_VECTOR_SIZE, ast_datastore::data, eprofiles_datastore::eprofiles, IS_GEOLOC_DS, and NULL.

Referenced by ast_geoloc_eprofiles_to_pidf(), and handle_outgoing_request().

◆ geoloc_channel_load()

int geoloc_channel_load ( void  )

Definition at line 316 of file geoloc_datastore.c.

317{
320}
struct ast_sorcery * geoloc_get_sorcery(void)
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70

References AST_MODULE_LOAD_SUCCESS, geoloc_get_sorcery(), and geoloc_sorcery.

Referenced by load_module().

◆ geoloc_channel_reload()

int geoloc_channel_reload ( void  )

Definition at line 322 of file geoloc_datastore.c.

323{
325}

References AST_MODULE_LOAD_SUCCESS.

Referenced by reload_module().

◆ geoloc_channel_unload()

int geoloc_channel_unload ( void  )

Definition at line 308 of file geoloc_datastore.c.

309{
310 if (geoloc_sorcery) {
312 }
314}
#define ast_sorcery_unref(sorcery)
Decrease the reference count of a sorcery structure.
Definition: sorcery.h:1500

References AST_MODULE_LOAD_SUCCESS, ast_sorcery_unref, and geoloc_sorcery.

Referenced by unload_module().

◆ geoloc_datastore_duplicate()

static void * geoloc_datastore_duplicate ( void *  obj)
static

Definition at line 45 of file geoloc_datastore.c.

46{
47 struct eprofiles_datastore *in_eds = obj;
48 struct eprofiles_datastore *out_eds;
49 int rc = 0;
50 int i = 0;
51 int eprofile_count = 0;
52
53 out_eds = ast_calloc(1, sizeof(*out_eds));
54 if (!out_eds) {
55 return NULL;
56 }
57
58 rc = AST_VECTOR_INIT(&out_eds->eprofiles, 2);
59 if (rc != 0) {
60 ast_free(out_eds);
61 return NULL;
62 }
63
64 eprofile_count = AST_VECTOR_SIZE(&in_eds->eprofiles);
65 for (i = 0; i < eprofile_count; i++) {
66 struct ast_geoloc_eprofile *ep = AST_VECTOR_GET(&in_eds->eprofiles, i);
67 rc = AST_VECTOR_APPEND(&out_eds->eprofiles, ao2_bump(ep));
68 if (rc != 0) {
69 /* This will clean up the bumped reference to the eprofile */
70 geoloc_datastore_free(out_eds);
71 return NULL;
72 }
73 }
74
75 return out_eds;
76}
#define ast_free(a)
Definition: astmm.h:180
static void geoloc_datastore_free(void *obj)

References ao2_bump, ast_calloc, ast_free, AST_VECTOR_APPEND, AST_VECTOR_GET, AST_VECTOR_INIT, AST_VECTOR_SIZE, eprofiles_datastore::eprofiles, geoloc_datastore_free(), and NULL.

◆ geoloc_datastore_free()

static void geoloc_datastore_free ( void *  obj)
static

Definition at line 36 of file geoloc_datastore.c.

37{
38 struct eprofiles_datastore *eds = obj;
39
42 ast_free(eds);
43}
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define AST_VECTOR_RESET(vec, cleanup)
Reset vector.
Definition: vector.h:625
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
Definition: vector.h:174

References ao2_cleanup, ast_free, AST_VECTOR_FREE, AST_VECTOR_RESET, and eprofiles_datastore::eprofiles.

Referenced by geoloc_datastore_duplicate().

Variable Documentation

◆ geoloc_datastore_info

const struct ast_datastore_info geoloc_datastore_info
static
Initial value:
= {
.type = GEOLOC_DS_TYPE,
}
static void * geoloc_datastore_duplicate(void *obj)
#define GEOLOC_DS_TYPE

Definition at line 78 of file geoloc_datastore.c.

Referenced by ast_geoloc_datastore_create(), and ast_geoloc_datastore_find().

◆ geoloc_sorcery

struct ast_sorcery* geoloc_sorcery