Shad Ansari | 2f7f9be | 2017-06-07 13:34:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | <:copyright-BRCM:2016:DUAL/GPL:standard |
| 3 | |
| 4 | Broadcom Proprietary and Confidential.(c) 2016 Broadcom |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Unless you and Broadcom execute a separate written software license |
| 8 | agreement governing use of this software, this software is licensed |
| 9 | to you under the terms of the GNU General Public License version 2 |
| 10 | (the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php, |
| 11 | with 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 | |
| 22 | Not withstanding the above, under no circumstances may you combine |
| 23 | this software in any way with any other Broadcom software provided |
| 24 | under a license other than the GPL, without Broadcom's express prior |
| 25 | written consent. |
| 26 | |
| 27 | :> |
| 28 | */ |
| 29 | |
| 30 | |
| 31 | #include <sys/socket.h> |
| 32 | #include <sys/un.h> |
| 33 | |
| 34 | #include <bcmos_system.h> |
| 35 | #include <bcmtr_plugin.h> |
| 36 | #include <bcm_config.h> |
| 37 | #include <bcmolt_tr_ud.h> |
| 38 | |
| 39 | char socket_path[] = SOCKET_PATH; |
| 40 | /* Plugin channel structure */ |
| 41 | typedef struct bcmtr_ud_channel |
| 42 | { |
| 43 | int device; /* Device */ |
| 44 | int sock; /* UD socket */ |
| 45 | } bcmtr_ud_channel; |
| 46 | |
| 47 | |
| 48 | /* This plugin stores both device id and per-device socket handle in |
| 49 | * transport's channel_id handle as |
| 50 | * (device_id << 24) | socket_handle. |
| 51 | * The following functions recover socket_handle and device_id from the channel_id handle |
| 52 | */ |
| 53 | |
| 54 | |
| 55 | /** Open communication channel */ |
| 56 | static bcmos_errno bcmtr_ud_open(int device, bcmtr_plugin_cfg *cfg, bcmtr_plugin_channel *ch) |
| 57 | { |
| 58 | struct sockaddr_un addr; |
| 59 | bcmtr_ud_channel *udch; |
| 60 | |
| 61 | udch = bcmos_calloc(sizeof(*udch)); |
| 62 | if (!udch) |
| 63 | { |
| 64 | BCMOS_TRACE_RETURN(BCM_ERR_NOMEM, "No memory\n"); |
| 65 | } |
| 66 | |
| 67 | if ( (udch->sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { |
| 68 | perror("socket error"); |
| 69 | exit(-1); |
| 70 | } |
| 71 | |
| 72 | memset(&addr, 0, sizeof(addr)); |
| 73 | addr.sun_family = AF_UNIX; |
| 74 | BUG_ON(sizeof(socket_path)>=sizeof(addr.sun_path)); |
| 75 | memcpy(addr.sun_path, socket_path, sizeof(socket_path)); |
| 76 | |
| 77 | if (connect(udch->sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) { |
| 78 | perror("connect error"); |
| 79 | exit(-1); |
| 80 | } |
| 81 | |
| 82 | bcmos_printf("Opened %d\n", udch->sock); |
| 83 | |
| 84 | udch->device = device; |
| 85 | |
| 86 | *ch = (bcmtr_plugin_channel)udch; |
| 87 | |
| 88 | return BCM_ERR_OK; |
| 89 | } |
| 90 | |
| 91 | /** Close communication channel */ |
| 92 | static bcmos_errno bcmtr_ud_close(bcmtr_plugin_channel ch) |
| 93 | { |
| 94 | bcmtr_ud_channel *udch = (bcmtr_ud_channel *)ch; |
| 95 | |
| 96 | bcmos_printf("Closed %d\n", udch->sock); |
| 97 | close(udch->sock); |
| 98 | bcmos_free(udch); |
| 99 | |
| 100 | return BCM_ERR_OK; |
| 101 | } |
| 102 | |
| 103 | /** Send data */ |
| 104 | static bcmos_errno bcmtr_ud_send(bcmtr_plugin_channel ch, bcmolt_subchannel subch, bcmolt_buf *buf, |
| 105 | bcmtr_send_flags flags) |
| 106 | { |
| 107 | bcmtr_ud_channel *udch = (bcmtr_ud_channel *)ch; |
| 108 | int buflen = bcmolt_buf_get_used(buf); |
| 109 | int len; |
| 110 | len = send(udch->sock, buf->start, buflen, 0); |
| 111 | |
| 112 | if (len < buflen) |
| 113 | { |
| 114 | bcmos_printf("%s: sock_sendmsg(%u) --> %d. errno=%d\n", __FUNCTION__, buflen, len, errno); |
| 115 | return BCM_ERR_COMM_FAIL; |
| 116 | } |
| 117 | |
| 118 | return BCM_ERR_OK; |
| 119 | } |
| 120 | |
| 121 | /** Receive data */ |
| 122 | static bcmos_errno bcmtr_ud_recv(bcmtr_plugin_channel ch, bcmolt_subchannel *subch, bcmolt_buf *buf) |
| 123 | { |
| 124 | bcmtr_ud_channel *udch = (bcmtr_ud_channel *)ch; |
| 125 | int len; |
| 126 | fd_set read_fds; |
| 127 | struct timeval tv; |
| 128 | bcmos_errno err; |
| 129 | int rc; |
| 130 | |
| 131 | FD_ZERO(&read_fds); |
| 132 | FD_SET(udch->sock, &read_fds); |
| 133 | tv.tv_sec = 0; |
| 134 | tv.tv_usec = BCMTR_MSG_TIMEOUT * 1000; |
| 135 | |
| 136 | rc = select(udch->sock + 1, &read_fds, NULL, NULL, &tv); |
| 137 | if (rc < 0) |
| 138 | { |
| 139 | perror("select"); |
| 140 | return BCM_ERR_COMM_FAIL; |
| 141 | } |
| 142 | if (!rc || !FD_ISSET(udch->sock, &read_fds)) |
| 143 | { |
| 144 | return BCM_ERR_TIMEOUT; |
| 145 | } |
| 146 | |
| 147 | err = bcmolt_buf_alloc(buf, BCMTR_MAX_MTU_SIZE, BCMOLT_BUF_ENDIAN_FIXED); |
| 148 | if (err) |
| 149 | return BCM_ERR_NOMEM; |
| 150 | |
| 151 | len = recv(udch->sock, buf->start, BCMTR_MAX_MTU_SIZE, 0); |
| 152 | if (len <= 0) |
| 153 | { |
| 154 | bcmolt_buf_free(buf); |
| 155 | return BCM_ERR_COMM_FAIL; |
| 156 | } |
| 157 | |
| 158 | bcmos_printf("Received %d bytes from dev_control\n", len); |
| 159 | |
| 160 | buf->len = len; |
| 161 | return BCM_ERR_OK; |
| 162 | } |
| 163 | |
| 164 | /** Initialize plugin callbacks |
| 165 | * \param[in,out] driver Transport plugin driver structure |
| 166 | * \return error code |
| 167 | */ |
| 168 | bcmos_errno bcmtr_ud_plugin_init(bcmtr_plugin_cfg *cfg, bcmtr_driver *driver) |
| 169 | { |
| 170 | bcmos_errno err = BCM_ERR_OK; |
| 171 | |
| 172 | bcmos_printf("Launching with a unix domain connection\n"); |
| 173 | |
| 174 | driver->open = bcmtr_ud_open; |
| 175 | driver->close = bcmtr_ud_close; |
| 176 | driver->recv = bcmtr_ud_recv; |
| 177 | driver->send = bcmtr_ud_send; |
| 178 | cfg->headroom = BCMUD_TXPREFIX_LEN; |
| 179 | |
| 180 | return err; |
| 181 | } |
| 182 | |