[Asterisk-code-review] res parking: Add dialplan functions for lot state and channel (asterisk[master])

Joshua Elson asteriskteam at digium.com
Fri Jul 6 17:02:27 CDT 2018


Joshua Elson has uploaded this change for review. ( https://gerrit.asterisk.org/9375


Change subject: res_parking: Add dialplan functions for lot state and channel
......................................................................

res_parking: Add dialplan functions for lot state and channel

This commit adds two new functions to res_parking.

The fist, CHECK_PARKING_SLOT, allows the determination of
whether a particular parking lot is in use from dialplan.

Additionally, GET_PARKING_SLOT_CHANNEL allows the retrieval
of the channel name of the channel occupying the parking slot.

ASTERISK-22825 #close

Change-Id: Idba6ae55b8a53f734238cb3d995cedb95c0e7b74
---
M res/parking/parking_bridge_features.c
1 file changed, 150 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/75/9375/1

diff --git a/res/parking/parking_bridge_features.c b/res/parking/parking_bridge_features.c
index 2770233..042222c 100644
--- a/res/parking/parking_bridge_features.c
+++ b/res/parking/parking_bridge_features.c
@@ -30,6 +30,7 @@
 #include "asterisk/astobj2.h"
 #include "asterisk/logger.h"
 #include "asterisk/pbx.h"
+#include "asterisk/app.h"
 #include "asterisk/bridge.h"
 #include "asterisk/bridge_internal.h"
 #include "asterisk/bridge_channel.h"
@@ -42,6 +43,40 @@
 #include "asterisk/core_local.h"
 #include "asterisk/causes.h"
 
+/*** DOCUMENTATION
+        <function name="CHECK_PARKING_SLOT" language="en_US">
+                <synopsis>
+                        Check whether a parking slot in a parking lot is currently occupied.
+                </synopsis>
+                <syntax>
+                        <parameter name="parking_space" required="true">
+                        </parameter>
+                        <parameter name="parking_lot" required="true">
+                        </parameter>
+                </syntax>
+                <description>
+                        <para>This function returns true if the current parking slot
+                        in the parking lot space is occupied.</para>
+                </description>
+        </function>
+        <function name="GET_PARKING_SLOT_CHANNEL" language="en_US">
+                <synopsis>
+                        Get the channel name of an occupied parking slot in a parking lot.
+                </synopsis>
+                <syntax>
+                        <parameter name="parking_space" required="true">
+                        </parameter>
+                        <parameter name="parking_lot" required="true">
+                        </parameter>
+                </syntax>
+                <description>
+                        <para>This function returns true if the current parking slot
+                        in the parking lot space is occupied.</para>
+                </description>
+        </function>
+***/
+
+
 struct parked_subscription_datastore {
 	struct stasis_subscription *parked_subscription;
 };
@@ -704,6 +739,116 @@
 	}
 }
 
+static int func_check_parking_slot (struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
+{
+	RAII_VAR(struct parking_lot *, lot, NULL, ao2_cleanup);
+	int space = -1;
+	int preferred_space = -1;
+	int ret = 0;
+
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(parking_space);
+		AST_APP_ARG(parking_lot);
+		AST_APP_ARG(other);
+	);
+
+	/* Parse the arguments. */
+	AST_STANDARD_APP_ARGS(args, data);
+
+	if (args.argc < 2) {
+		/* Didn't receive enough arguments to do anything */
+		ast_log(LOG_ERROR,
+			"Usage: %s(<parking_space>,<parking_lot>)\n",
+			function);
+		return -1;
+	}
+
+	lot = parking_lot_find_by_name(args.parking_lot);
+	if (!lot) {
+		ast_log(LOG_ERROR, "Could not find parking lot: '%s'\n", args.parking_lot);
+		return -1;
+	}
+
+	if (ast_strlen_zero(args.parking_space) || sscanf(args.parking_space, "%30d", &space) != 1) {
+		return -1;
+	}
+
+	ao2_lock(lot);
+	preferred_space = parking_lot_get_space(lot, space);
+	ao2_unlock(lot);
+	
+	if (space == -1 || preferred_space != space) {
+		ret = 1; /* Lot is full */
+	} else {
+		ret = 0;
+	}
+
+	ast_copy_string(buf, ret ? "1" : "0" , len);
+
+	return 0;	
+
+}
+
+/*! \brief  Dial plan function to get the parking lot channel of an occupied parking lot*/
+static int func_get_parkingslot_channel(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
+{
+	RAII_VAR(struct parked_user *, pu, NULL, ao2_cleanup);
+	RAII_VAR(struct parking_lot *, lot, NULL, ao2_cleanup);
+	int space = -1;
+	const char *content = NULL;
+
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(parking_space);
+		AST_APP_ARG(parking_lot);
+		AST_APP_ARG(other);
+	);
+
+	/* Parse the arguments. */
+	AST_STANDARD_APP_ARGS(args, data);
+
+	if (args.argc < 2) {
+		/* Didn't receive enough arguments to do anything */
+		ast_log(LOG_ERROR,
+			"Usage: %s(<parking_space>,<parking_lot>)\n",
+			function);
+		return -1;
+	}
+
+	lot = parking_lot_find_by_name(args.parking_lot);
+	if (!lot) {
+		ast_log(LOG_ERROR, "Could not find parking lot: '%s'\n", args.parking_lot);
+		return -1;
+	}
+
+        if (!ast_strlen_zero(args.parking_space)) {
+                if (sscanf(args.parking_space, "%d", &space) != 1 || space < 0) {
+                        ast_log(LOG_ERROR, "value '%s' for parking_space argument is invalid. Must be an integer greater than 0.\n", args.parking_space);
+                        return -1;
+                }
+        }
+
+        pu = parking_lot_retrieve_parked_user(lot, space);
+        if (!pu) {
+                return -1;
+        }
+
+	content = ast_channel_name(pu->chan);
+	ast_copy_string(buf, content, len);
+
+	return 0;	
+
+}
+
+static struct ast_custom_function getparkingslotchannel_function = {
+        .name = "GET_PARKING_SLOT_CHANNEL",
+        .read = func_get_parkingslot_channel,
+};
+
+static struct ast_custom_function check_parking_slot_function = {
+	.name = "CHECK_PARKING_SLOT",
+	.read = func_check_parking_slot,
+};
+
 struct ast_parking_bridge_feature_fn_table parking_provider = {
 	.module_version = PARKING_MODULE_VERSION,
 	.module_name = __FILE__,
@@ -717,12 +862,17 @@
 {
 	ast_bridge_features_unregister(AST_BRIDGE_BUILTIN_PARKCALL);
 	ast_parking_unregister_bridge_features(parking_provider.module_name);
+	ast_custom_function_unregister(&check_parking_slot_function);
+	ast_custom_function_unregister(&getparkingslotchannel_function);
 }
 
 int load_parking_bridge_features(void)
 {
 	parking_provider.module = AST_MODULE_SELF;
 
+	ast_custom_function_register(&check_parking_slot_function);
+	ast_custom_function_register(&getparkingslotchannel_function);
+
 	if (ast_parking_register_bridge_features(&parking_provider)) {
 		return -1;
 	}

-- 
To view, visit https://gerrit.asterisk.org/9375
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Idba6ae55b8a53f734238cb3d995cedb95c0e7b74
Gerrit-Change-Number: 9375
Gerrit-PatchSet: 1
Gerrit-Owner: Joshua Elson <joshelson at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180706/3e3f6ef5/attachment-0001.html>


More information about the asterisk-code-review mailing list