summaryrefslogtreecommitdiffstats
path: root/src/ahl-deviceenum.c
blob: 7e67d7a3e66b10520b357a499d6e4f0fc4a0a6f3 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
# Installing XDS server

Depending of your configuration, this step is necessary or not.

IOW **you are a developer and plan to use/connect to an existing `xds-server`**
running on your local network (On-Premise config) or in the Cloud (SaaS config),
**you don't need to install the server part and you can skip this step**.

For others (standalone config or administrators that want to install an
On-Premise solution) xds-server must be installed.

Several installation types are supported :

| Install type | Supported OS | Section to refer |
|--------------|--------------|------------------|
| Container | Linux or MacOS | [see Installation based on Docker container](#docker_container) |
| Virtual Machine | Linux, MacOS or Windows | [see Installation based on VirtualBox appliance](#vbox_appliance) |
| Native | Linux | [see Native installation](#native) |

## <a name="docker_container"></a> Installation based on Docker container

### Prerequisites

Docker is installed on the host machine, please refer to [Docker documentation](https://docs.docker.com/engine/installation/) for more details.

### Get the container

Load the pre-build AGL SDK docker image including `xds-server`:

```bash
seb@laptop ~$ wget -O - http://iot.bzh/download/public/2017/XDS/docker/docker_agl_worker-xds-latest.tar.xz | docker load
```

You should get `docker.automotivelinux.org/agl/worker-xds:X.Y` image

```bash
# List image that we just load
seb@laptop ~$ docker images "docker.automotivelinux.org/agl/worker-xds*"

docker.automotivelinux.org/agl/worker-xds       4.0              786d65b2792c        6 days ago          654MB
```

### Create and start a new container

Use provided script to create a new docker container and start it:

```bash
# Get script
seb@laptop ~$ wget https://raw.githubusercontent.com/iotbzh/xds-server/master/scripts/xds-docker-create-container.sh

# Create new XDS worker container
seb@laptop ~$ bash ./xds-docker-create-container.sh

# Check that new container is running
seb@laptop ~$ docker ps | grep worker-xds

b985d81af40c        docker.automotivelinux.org/agl/worker-xds:3.99.1       "/usr/bin/wait_for..."   6 days ago           Up 4 hours          0.0.0.0:8000->8000/tcp, 0.0.0.0:69->69/udp, 0.0.0.0:10809->10809/tcp, 0.0.0.0:2222->22/tcp    agl-xds-seb@laptop-0-seb
```

### Check if xds-server is running

`xds-server` is automatically started as a service on container startup.

To check if xds-server is correctly install and running, you can access the web
interface, what we call the "Dashboard", using a web browser :

```bash
# if container is running on your local host
# (else replace localhost by the name or the ip of the machine running the container)
seb@laptop ~$ xdg-open http://localhost:8000
```

### Container settings

This container (ID=0) exposes following ports:

- 8000 : `xds-server` to serve XDS Dashboard
- 69   : TFTP
- 2222 : ssh

This container also creates the following volumes (sharing directories between
inside and outside docker):

| Directory on host | Directory inside docker | Comment |
|-------------------|-------------------------|---------|
| $HOME/xds-workspace | /home/devel/xds-workspace | XDS projects workspace location|
| $HOME/ssd/xdt_0 | /xdt | location to store SDKs |
| $HOME/devel/docker/share |/home/devel/share | another shared directory |

<!-- note -->
Please refer to **part 2 - xds-server** documentation for additional info.
<!-- endnote -->

## <a name="vbox_appliance"></a> Installation based on VirtualBox appliance

_coming soon ..._

## <a name="native"></a> Native installation

<!-- note -->
Only Linux host OS is supported and tested for native installation !
<!-- endnote -->

### Install packages for debian distro type

```bash
# 'DISTRO' can be set to { xUbuntu_16.04, xUbuntu_16.10, xUbuntu_17.04, Debian_8.0, Debian_9.0}
seb@laptop ~$  export DISTRO="xUbuntu_16.04"

seb@laptop ~$  wget -O - http://download.opensuse.org/repositories/isv:/LinuxAutomotive:/app-Development/${DISTRO}/Release.key | sudo apt-key add -
seb@laptop ~$  sudo bash -c "cat >> /etc/apt/sources.list.d/AGL.list <<EOF
deb http://download.opensuse.org/repositories/isv:/LinuxAutomotive:/app-Development/${DISTRO}/ ./
EOF"

seb@laptop ~$  sudo apt-get update
seb@laptop ~$  sudo apt-get install agl-xds-server
```

### Install packages for openSUSE distro type

```bash
# DISTRO can be set to {openSUSE_Leap_42.2, openSUSE_Leap_42.3, openSUSE_Tumbleweed}
seb@laptop ~$  export DISTRO="openSUSE_Leap_42.2"

seb@laptop ~$  sudo zypper ar http://download.opensuse.org/repositories/isv:/LinuxAutomotive:/app-Development/${DISTRO}/isv:LinuxAutomotive:app-Development.repo

seb@laptop ~$  sudo zypper ref
seb@laptop ~$  sudo zypper install agl-xds-server
```

### Configure xds-server

<!-- note -->
>**Optional step**: nothing to do if you keep default settings
<!-- endnote -->

When `xds-server` is started as a systemd service, default environment variables
are set into `/etc/default/xds-server` file.

`xds-server` configuration is also driven by a JSON config file (`config.json`),
and default JSON config is `/etc/xds-server/config.json`.

<!-- note -->
>**Note**: you can use your own JSON config by settings `APP_CONFIG` variable of
`/etc/default/xds-server` file to your file, for example
`/home/MYUSER/.xds/server/config.json`
<!-- endnote -->

Supported fields in JSON configuration file are :

- **httpPort** : HTTP port of client webapp / dashboard
- **webAppDir** : location of client dashboard (default: webapp/dist)
- **shareRootDir** : root directory where projects will be copied
- **logsDir**  : directory to store logs (eg. syncthing output)
- **sdkRootDir** : root directory where cross SDKs are installed
- **syncthing.binDir** : syncthing binaries directory (default: executable directory)
- **syncthing.home"** : syncthing home directory (usually .../syncthing-config)
- **syncthing.gui-address** : syncthing gui url (default http://localhost:8384)
- **syncthing.gui-apikey** : syncthing api-key to use (default auto-generated)

All fields are optional and example below corresponds to the default values:

```json
{
    "httpPort": 8000,
    "webAppDir": "webapp/dist",
    "shareRootDir": "${HOME}/.xds-server/projects",
    "logsDir": "/tmp/logs",
    "sdkRootDir": "/xdt/sdk",
    "syncthing": {
        "binDir": "./bin",
        "home": "${HOME}/.xds-server/syncthing-config",
        "gui-address": "http://localhost:8384",
        "gui-apikey": "123456789",
    }
}
```

>**NOTE:** environment variables are supported by using `${MY_VAR}` syntax.

### Start/Stop xds-server

`xds-server` can be managed as a systemd service with the following commands:

```bash
# Status XDS server:
seb@laptop ~$ sudo systemctl status xds-server.service

# Stop XDS server
seb@laptop ~$ sudo systemctl stop xds-server.service

# Start XDS server
seb@laptop ~$ sudo systemctl start xds-server.service

# Get XDS server logs
seb@laptop ~$ sudo journalctl --unit=xds-server.service --output=cat
```

To check if xds-server is correctly install and running, you can access the web
interface, what we call the "Dashboard", using a web browser :

```bash
seb@laptop ~$ xdg-open http://localhost:8000
```
>(const char * in_pDomainStr) { return (int) (strcasecmp(in_pDomainStr,AHL_DOMAIN_EXTERNAL) == 0); } static int FillALSAPCMInfo( snd_pcm_t * in_pPcmHandle, EndpointInfoT * out_pEndpointInfo ) { g_assert_nonnull(in_pPcmHandle); g_assert_nonnull(out_pEndpointInfo); snd_pcm_type_t pcmType = 0; snd_pcm_info_t * pPcmInfo = NULL; int iAlsaRet = 0; const char * pCardName = NULL; int retVal = 0; snd_ctl_t * ctlHandle = NULL; snd_ctl_card_info_t * ctlInfo = NULL; snd_pcm_info_alloca(&pPcmInfo); snd_ctl_card_info_alloca(&ctlInfo); // retrieve PCM type pcmType = snd_pcm_type(in_pPcmHandle); switch (pcmType) { case SND_PCM_TYPE_HW: out_pEndpointInfo->deviceURIType = DEVICEURITYPE_ALSA_HW; break; case SND_PCM_TYPE_DMIX: out_pEndpointInfo->deviceURIType = DEVICEURITYPE_ALSA_DMIX; break; case SND_PCM_TYPE_SOFTVOL: out_pEndpointInfo->deviceURIType = DEVICEURITYPE_ALSA_SOFTVOL; break; case SND_PCM_TYPE_PLUG: out_pEndpointInfo->deviceURIType = DEVICEURITYPE_ALSA_PLUG; break; default: out_pEndpointInfo->deviceURIType = DEVICEURITYPE_ALSA_OTHER; break; } iAlsaRet = snd_pcm_info(in_pPcmHandle,pPcmInfo); if (iAlsaRet < 0) { AFB_WARNING("Error retrieving PCM device info"); return 1; } // get card number out_pEndpointInfo->alsaInfo.cardNum = snd_pcm_info_get_card(pPcmInfo); if ( out_pEndpointInfo->alsaInfo.cardNum < 0 ) { AFB_WARNING("No Alsa card number available"); return 1; } // get device number out_pEndpointInfo->alsaInfo.deviceNum = snd_pcm_info_get_device(pPcmInfo); if ( out_pEndpointInfo->alsaInfo.deviceNum < 0 ) { AFB_WARNING("No Alsa device number available"); return 1; } // get sub-device number out_pEndpointInfo->alsaInfo.subDeviceNum = snd_pcm_info_get_subdevice(pPcmInfo); if ( out_pEndpointInfo->alsaInfo.subDeviceNum < 0 ) { AFB_WARNING("No Alsa subdevice number available"); return 1; } char cardName[32]; sprintf(cardName, "hw:%d", out_pEndpointInfo->alsaInfo.cardNum); iAlsaRet = snd_ctl_open(&ctlHandle, cardName, 0); if ( iAlsaRet < 0 ) { AFB_WARNING("Could not open ALSA card control"); return 1; } iAlsaRet = snd_ctl_card_info(ctlHandle, ctlInfo); if ( iAlsaRet < 0 ) { AFB_WARNING("Could not retrieve ALSA card info"); snd_ctl_close(ctlHandle); return 1; } // Populate unique target card name pCardName = snd_ctl_card_info_get_id(ctlInfo); if (pCardName == NULL) { AFB_WARNING("No Alsa card name available"); snd_ctl_close(ctlHandle); return 1; } out_pEndpointInfo->gsDeviceName = g_strdup(pCardName); snd_ctl_close(ctlHandle); return retVal; } EndpointInfoT * InitEndpointInfo() { EndpointInfoT * pEndpointInfo = (EndpointInfoT*) malloc(sizeof(EndpointInfoT)); memset(pEndpointInfo,0,sizeof(EndpointInfoT)); pEndpointInfo->endpointID = AHL_UNDEFINED; pEndpointInfo->type = ENDPOINTTYPE_MAXVALUE; pEndpointInfo->deviceURIType = DEVICEURITYPE_MAXVALUE; pEndpointInfo->alsaInfo.cardNum = AHL_UNDEFINED; pEndpointInfo->alsaInfo.deviceNum = AHL_UNDEFINED; pEndpointInfo->alsaInfo.subDeviceNum = AHL_UNDEFINED; pEndpointInfo->format.sampleRate = AHL_UNDEFINED; pEndpointInfo->format.numChannels = AHL_UNDEFINED; pEndpointInfo->format.sampleType = AHL_FORMAT_UNKNOWN; pEndpointInfo->pPropTable = g_hash_table_new(g_str_hash, g_str_equal); return pEndpointInfo; } void TermEndpointInfo( EndpointInfoT * out_pEndpointInfo ) { #define SAFE_FREE(__ptr__) if(__ptr__) g_free(__ptr__); __ptr__ = NULL; SAFE_FREE(out_pEndpointInfo->gsDeviceName); SAFE_FREE(out_pEndpointInfo->gsDisplayName); SAFE_FREE(out_pEndpointInfo->gsDeviceDomain); SAFE_FREE(out_pEndpointInfo->pRoleName); SAFE_FREE(out_pEndpointInfo->gsDeviceURI); SAFE_FREE(out_pEndpointInfo->gsHALAPIName); if (out_pEndpointInfo->pPropTable) { // Free json_object for all property values GHashTableIter iter; gpointer key, value; g_hash_table_iter_init (&iter, out_pEndpointInfo->pPropTable); while (g_hash_table_iter_next (&iter, &key, &value)) { if (value) json_object_put(value); } g_hash_table_remove_all(out_pEndpointInfo->pPropTable); g_hash_table_destroy(out_pEndpointInfo->pPropTable); out_pEndpointInfo->pPropTable = NULL; } // GLib automatically frees item when removed from the array } // For a given audio role int EnumerateDevices(json_object * in_jDeviceArray, char * in_pAudioRole, EndpointTypeT in_deviceType, GPtrArray * out_pEndpointArray) { g_assert_nonnull(in_jDeviceArray); int iNumberDevices = json_object_array_length(in_jDeviceArray); // Parse and validate list of available devices for (int i = 0; i < iNumberDevices; i++) { char * pDeviceURIDomain = NULL; char * pFullDeviceURI = NULL; char * pDeviceURIPCM = NULL; int err = 0; json_object * jDevice = json_object_array_get_idx(in_jDeviceArray,i); if (jDevice == NULL) { AFB_WARNING("Invalid device array -> %s",json_object_to_json_string(in_jDeviceArray)); continue; } // strip domain name from URI pFullDeviceURI = (char *)json_object_get_string(jDevice); char * pFullDeviceURICopy = g_strdup(pFullDeviceURI); // strtok is destructive err = SeparateDomainFromDeviceURI(pFullDeviceURICopy,&pDeviceURIDomain,&pDeviceURIPCM); if (err) { AFB_WARNING("Invalid device URI string -> %s",pFullDeviceURICopy); continue; } EndpointInfoT * pEndpointInfo = InitEndpointInfo(); g_assert_nonnull(pEndpointInfo); // non ALSA URI are simply passed to application (no validation) at this time // In Non ALSA case devices in config are assumed to be available, if not application can fallback on explicit device selection pEndpointInfo->gsDeviceName = g_strdup(pDeviceURIPCM); pEndpointInfo->gsDeviceDomain = g_strdup(pDeviceURIDomain); pEndpointInfo->gsDeviceURI = g_strdup(pDeviceURIPCM); pEndpointInfo->pRoleName = g_strdup(in_pAudioRole); g_free(pFullDeviceURICopy); pFullDeviceURICopy = NULL; pDeviceURIDomain = NULL; //Derived from above mem pDeviceURIPCM = NULL; //Derived from above mem if (IsAlsaDomain(pEndpointInfo->gsDeviceDomain)) { // TODO: Missing support for loose name matching // This will require using ALSA hints to get PCM names // And would iterate over all available devices matching string (possibly all if no filtering is desired for a certain role) // Get PCM handle snd_pcm_t * pPcmHandle = NULL; snd_pcm_stream_t streamType = in_deviceType == ENDPOINTTYPE_SOURCE ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK; err = snd_pcm_open(&pPcmHandle, pEndpointInfo->gsDeviceURI, streamType, 0); if (err < 0) { AFB_NOTICE("Alsa PCM device was not found -> %s", pEndpointInfo->gsDeviceURI); continue; } err = FillALSAPCMInfo(pPcmHandle,pEndpointInfo); if (err) { AFB_WARNING("Unable to retrieve PCM information for PCM -> %s",pEndpointInfo->gsDeviceURI); snd_pcm_close(pPcmHandle); continue; } snd_pcm_close(pPcmHandle); } else if (IsPulseDomain(pEndpointInfo->gsDeviceDomain)) { // Pulse domain // For now display name is device URI directly, could extrapolated using more heuristics or even usins Pulse API later on pEndpointInfo->deviceURIType = DEVICEURITYPE_NOT_ALSA; } else if (IsGStreamerDomain(pEndpointInfo->gsDeviceDomain)){ // GStreamer domain // For now display name is device URI directly, could extrapolated using more heuristics or even usins GStreamer API later on pEndpointInfo->deviceURIType = DEVICEURITYPE_NOT_ALSA; } else if (IsExternalDomain(pEndpointInfo->gsDeviceDomain)){ // External domain pEndpointInfo->deviceURIType = DEVICEURITYPE_NOT_ALSA; } else { // Unknown domain AFB_WARNING("Unknown domain in device URI string -> %s",pFullDeviceURI); continue; } pEndpointInfo->endpointID = in_deviceType == ENDPOINTTYPE_SOURCE ? CreateNewSourceID() : CreateNewSinkID(); pEndpointInfo->type = in_deviceType; // Assigned by policy initialization pEndpointInfo->gsDisplayName = malloc(AHL_STR_MAX_LENGTH*sizeof(char)); memset(pEndpointInfo->gsDisplayName,0,AHL_STR_MAX_LENGTH*sizeof(char)); pEndpointInfo->gsHALAPIName = malloc(AHL_STR_MAX_LENGTH*sizeof(char)); memset(pEndpointInfo->gsDisplayName,0,AHL_STR_MAX_LENGTH*sizeof(char)); // add to structure to list of available devices g_ptr_array_add(out_pEndpointArray, pEndpointInfo); } // for all devices AFB_DEBUG ("Audio high-level - Enumerate devices done"); return 0; }