blob: c9453832855068538fc443d1392c9cca1d634a4b [file] [log] [blame]
Shad Ansari2f7f9be2017-06-07 13:34:53 -07001/*
2<:copyright-BRCM:2016:DUAL/GPL:standard
3
4 Broadcom Proprietary and Confidential.(c) 2016 Broadcom
5 All Rights Reserved
6
7Unless you and Broadcom execute a separate written software license
8agreement governing use of this software, this software is licensed
9to you under the terms of the GNU General Public License version 2
10(the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php,
11with the following added to such license:
12
13 As a special exception, the copyright holders of this software give
14 you permission to link this software with independent modules, and
15 to copy and distribute the resulting executable under terms of your
16 choice, provided that you also meet, for each linked independent
17 module, the terms and conditions of the license of that module.
18 An independent module is a module which is not derived from this
19 software. The special exception does not apply to any modifications
20 of the software.
21
22Not withstanding the above, under no circumstances may you combine
23this software in any way with any other Broadcom software provided
24under a license other than the GPL, without Broadcom's express prior
25written consent.
26
27:>
28 */
29
30/*
31 * bcmolt_dev_selector.c
32 * Set / get current device for all CLI commands
33 */
34#include <bcmos_system.h>
35#include <bcmcli.h>
36#include <bcmolt_dev_selector.h>
37
38/* Current device. External variable */
39
40/* Device change callback registration structure */
41typedef struct dev_sel_subscriber dev_sel_subscriber;
42struct dev_sel_subscriber
43{
44 F_current_device_change_ind cb; /* Subscriber's callback */
45 STAILQ_ENTRY(dev_sel_subscriber) next;
46};
47
48static STAILQ_HEAD(dev_sel_subscriber_list, dev_sel_subscriber) subscriber_list;
49
50/* Get/set the current device CLI handler
51 */
52static bcmos_errno _dev_sel_device_handler(bcmcli_session *session, const bcmcli_cmd_parm parm[], uint16_t nparms)
53{
54 /* If current device was set - notify all subscribers.
55 * Do not check if it changed here. Sometimes it is good to be able
56 * to refresh.
57 */
58 if (nparms)
59 {
60 dev_sel_subscriber *subs;
61
62 current_device = parm[0].value.number;
63
64 /* Notify all subscribers */
65 STAILQ_FOREACH(subs, &subscriber_list, next)
66 {
67 subs->cb(session, current_device);
68 }
69 }
70
71 bcmcli_session_print(session, "Current device: %ld\n", (long)current_device);
72
73 return BCM_ERR_OK;
74}
75
76
77/* Register for current device change notification.
78 * Multiple clients can register.
79 */
80bcmos_errno bcmolt_dev_sel_ind_register(F_current_device_change_ind cb)
81{
82 dev_sel_subscriber *subs = bcmos_calloc(sizeof(dev_sel_subscriber));
83
84 if (!subs)
85 return BCM_ERR_NOMEM;
86
87 subs->cb = cb;
88 STAILQ_INSERT_TAIL(&subscriber_list, subs, next);
89
90 return BCM_ERR_OK;
91}
92
93/* Initialize device selector module */
94bcmos_errno bcmolt_dev_sel_init(bcmcli_entry *parent_dir)
95{
96 STAILQ_INIT(&subscriber_list);
97
98 /* Get/set current device */
99 BCMCLI_MAKE_CMD(parent_dir, "device", "Get/Set the current device", _dev_sel_device_handler,
100 BCMCLI_MAKE_PARM_RANGE("device", "Device index", BCMCLI_PARM_NUMBER, BCMCLI_PARM_FLAG_OPTIONAL,
101 0, BCMTR_MAX_OLTS-1));
102
103 return BCM_ERR_OK;
104}
105
106