summaryrefslogtreecommitdiff
path: root/res/parking/parking_devicestate.c
blob: 3f6d07d515fd07e54d48dc0ee85cbae02e784bd2 (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
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2013, Digium, Inc.
 *
 * Jonathan Rose <jrose@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Call Parking Device State Management
 *
 * \author Jonathan Rose <jrose@digium.com>
 */

#include "asterisk.h"
#include "asterisk/logger.h"
#include "res_parking.h"
#include "asterisk/devicestate.h"

struct parking_lot_extension_inuse_search {
	char *context;
	int exten;
};

static int retrieve_parked_user_targeted(void *obj, void *arg, int flags)
{
	int *target = arg;
	struct parked_user *user = obj;
	if (user->parking_space == *target) {
		return CMP_MATCH;
	}

	return 0;
}

static int parking_lot_search_context_extension_inuse(void *obj, void *arg, int flags)
{
	struct parking_lot *lot = obj;
	struct parking_lot_extension_inuse_search *search = arg;
	RAII_VAR(struct parked_user *, user, NULL, ao2_cleanup);

	if (strcmp(lot->cfg->parking_con, search->context)) {
		return 0;
	}

	if ((search->exten < lot->cfg->parking_start) || (search->exten > lot->cfg->parking_stop)) {
		return 0;
	}

	user = ao2_callback(lot->parked_users, 0, retrieve_parked_user_targeted, &search->exten);
	if (!user) {
		return 0;
	}

	ao2_lock(user);
	if (user->resolution != PARK_UNSET) {
		/* The parked user isn't in an answerable state. */
		ao2_unlock(user);
		return 0;
	}
	ao2_unlock(user);

	return CMP_MATCH;
}

static enum ast_device_state metermaidstate(const char *data)
{
	struct ao2_container *global_lots = get_parking_lot_container();
	RAII_VAR(struct parking_lot *, lot, NULL, ao2_cleanup);
	char *context;
	char *exten;
	struct parking_lot_extension_inuse_search search = {};

	context = ast_strdupa(data);

	exten = strsep(&context, "@");

	if (ast_strlen_zero(context) || ast_strlen_zero(exten)) {
		return AST_DEVICE_INVALID;
	}

	search.context = context;
	if (sscanf(exten, "%d", &search.exten) != 1) {
		return AST_DEVICE_INVALID;
	}

	ast_debug(4, "Checking state of exten %d in context %s\n", search.exten, context);

	lot = ao2_callback(global_lots, 0, parking_lot_search_context_extension_inuse, &data);
	if (!lot) {
		return AST_DEVICE_NOT_INUSE;
	}

	return AST_DEVICE_INUSE;
}

void parking_notify_metermaids(int exten, const char *context, enum ast_device_state state)
{
	ast_debug(4, "Notification of state change to metermaids %d@%s\n to state '%s'\n",
		exten, context, ast_devstate2str(state));

	ast_devstate_changed(state, AST_DEVSTATE_CACHABLE, "park:%d@%s", exten, context);
}

void unload_parking_devstate(void)
{
	ast_devstate_prov_del("Park");
}

int load_parking_devstate(void)
{
	return ast_devstate_prov_add("Park", metermaidstate);
}