diff options
Diffstat (limited to 'bindings')
31 files changed, 5500 insertions, 0 deletions
diff --git a/bindings/CMakeLists.txt b/bindings/CMakeLists.txt new file mode 100644 index 00000000..2aacf1a1 --- /dev/null +++ b/bindings/CMakeLists.txt @@ -0,0 +1,5 @@ +ADD_SUBDIRECTORY(samples) +ADD_SUBDIRECTORY(audio) +ADD_SUBDIRECTORY(radio) +ADD_SUBDIRECTORY(media) +ADD_SUBDIRECTORY(intrinsics) diff --git a/bindings/audio/CMakeLists.txt b/bindings/audio/CMakeLists.txt new file mode 100644 index 00000000..11da28e1 --- /dev/null +++ b/bindings/audio/CMakeLists.txt @@ -0,0 +1,27 @@ +INCLUDE(FindPkgConfig) +PKG_CHECK_MODULES(alsa alsa) +PKG_CHECK_MODULES(pulseaudio libpulse libpulse-simple) +INCLUDE(FindThreads) +FIND_PACKAGE(Threads) + +IF(alsa_FOUND) + + MESSAGE(STATUS "ALSA found ; will compile Audio binding... (BINDING)") + + IF(pulseaudio_FOUND) + MESSAGE(STATUS "PulseAudio found ; Audio binding will have PulseAudio support") + ADD_DEFINITIONS(-DHAVE_PULSE=1) + SET(pulse_sources audio-pulse.c) + ENDIF(pulseaudio_FOUND) + + INCLUDE_DIRECTORIES(${include_dirs} ${alsa_INCLUDE_DIRS} ${pulseaudio_INCLUDE_DIRS}) + ADD_LIBRARY(audio-api MODULE audio-api.c audio-alsa.c ${pulse_sources}) + SET_TARGET_PROPERTIES(audio-api PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" + ) + TARGET_LINK_LIBRARIES(audio-api ${link_libraries} ${alsa_LIBRARIES} ${pulseaudio_LIBRARIES}) + INSTALL(TARGETS audio-api + LIBRARY DESTINATION ${binding_install_dir}) + +ENDIF(alsa_FOUND) diff --git a/bindings/audio/audio-alsa.c b/bindings/audio/audio-alsa.c new file mode 100644 index 00000000..45ff2edb --- /dev/null +++ b/bindings/audio/audio-alsa.c @@ -0,0 +1,311 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "audio-api.h" +#include "audio-alsa.h" + +snd_mixer_selem_channel_id_t SCHANNELS[8] = { + SND_MIXER_SCHN_FRONT_LEFT, + SND_MIXER_SCHN_FRONT_RIGHT, + SND_MIXER_SCHN_FRONT_CENTER, + SND_MIXER_SCHN_REAR_LEFT, + SND_MIXER_SCHN_REAR_RIGHT, + SND_MIXER_SCHN_REAR_CENTER, + SND_MIXER_SCHN_SIDE_LEFT, + SND_MIXER_SCHN_SIDE_RIGHT +}; + +static struct dev_ctx_alsa **dev_ctx_a = NULL; + + +unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) { + + snd_pcm_t *dev; + snd_pcm_hw_params_t *params; + snd_mixer_t *mixer; + snd_mixer_selem_id_t *mixer_sid; + snd_mixer_elem_t *mixer_elm; + snd_mixer_elem_t *mixer_elm_m; + unsigned int rate = 22050; + long vol, vol_min, vol_max; + int num, i; + + if (snd_pcm_open (&dev, name, SND_PCM_STREAM_PLAYBACK, 0) < 0) { + fprintf (stderr, "ALSA backend could not open card '%s'\n", name); + return 0; + } + + snd_pcm_hw_params_malloc (¶ms); + snd_pcm_hw_params_any (dev, params); + snd_pcm_hw_params_set_access (dev, params, SND_PCM_ACCESS_RW_INTERLEAVED); + snd_pcm_hw_params_set_format (dev, params, SND_PCM_FORMAT_S16_LE); + snd_pcm_hw_params_set_rate_near (dev, params, &rate, 0); + snd_pcm_hw_params_set_channels (dev, params, ctx->channels); + if (snd_pcm_hw_params (dev, params) < 0) { + snd_pcm_hw_params_free (params); + fprintf (stderr, "ALSA backend could set channels on card '%s'\n", name); + return 0; + } + snd_pcm_prepare (dev); + + snd_mixer_open (&mixer, 0); + if (snd_mixer_attach (mixer, name) < 0) { + snd_pcm_hw_params_free (params); + fprintf (stderr, "ALSA backend could not open mixer for card '%s'\n", name); + return 0; + } + snd_mixer_selem_register (mixer, NULL, NULL); + snd_mixer_load (mixer); + + snd_mixer_selem_id_alloca (&mixer_sid); + snd_mixer_selem_id_set_index (mixer_sid, 0); + snd_mixer_selem_id_set_name (mixer_sid, "Master"); + + mixer_elm = snd_mixer_find_selem (mixer, mixer_sid); + mixer_elm_m = NULL; + + if (!mixer_elm) { + /* no "Master" mixer ; we are probably on a board... search ! */ + for (mixer_elm = snd_mixer_first_elem (mixer); mixer_elm != NULL; + mixer_elm = snd_mixer_elem_next (mixer_elm)) { + if (snd_mixer_elem_info (mixer_elm) < 0) + continue; + snd_mixer_selem_get_id (mixer_elm, mixer_sid); + if (strstr (snd_mixer_selem_id_get_name (mixer_sid), "DVC Out")) { + + /* this is Porter... let us found the specific mute switch */ + snd_mixer_selem_id_set_index (mixer_sid, 0); + snd_mixer_selem_id_set_name (mixer_sid, "DVC Out Mute"); + mixer_elm_m = snd_mixer_find_selem (mixer, mixer_sid); + + break; + } + } + } + + if (mixer_elm) { + snd_mixer_selem_get_playback_volume_range (mixer_elm, &vol_min, &vol_max); + snd_mixer_selem_get_playback_volume (mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &vol); + } + + /* allocate the global array if it hasn't been done */ + if (!dev_ctx_a) { + dev_ctx_a = (dev_ctx_alsa_T**) malloc (sizeof(dev_ctx_alsa_T*)); + dev_ctx_a[0] = (dev_ctx_alsa_T*) malloc (sizeof(dev_ctx_alsa_T)); + dev_ctx_a[0]->name = NULL; + dev_ctx_a[0]->dev = NULL; + } + + /* is a card with similar name already opened ? */ + for (num = 0; num < (sizeof(dev_ctx_a)/sizeof(dev_ctx_alsa_T*)); num++) { + if (dev_ctx_a[num]->name && + !strcmp (dev_ctx_a[num]->name, name)) { + fprintf (stderr, "Card '%s' already locked by other ALSA backend session\n", name); + return 0; + } + } + num--; + + /* it's not... let us add it to the global array */ + dev_ctx_a = (dev_ctx_alsa_T**) realloc (dev_ctx_a, (num+1)*sizeof(dev_ctx_alsa_T*)); + if (!dev_ctx_a[num]) + dev_ctx_a[num] = (dev_ctx_alsa_T*) malloc (sizeof(dev_ctx_alsa_T)); + dev_ctx_a[num]->name = strdup (name); + dev_ctx_a[num]->dev = dev; + dev_ctx_a[num]->params = params; + dev_ctx_a[num]->mixer_elm = mixer_elm; + dev_ctx_a[num]->mixer_elm_m = mixer_elm_m; + dev_ctx_a[num]->vol_max = vol_max; + dev_ctx_a[num]->vol = vol; + dev_ctx_a[num]->thr_should_run = 0; + dev_ctx_a[num]->thr_finished = 0; + + /* make the client context aware of current card state */ + for (i = 0; i < 8; i++) + ctx->volume[i] = _alsa_get_volume (num, i); + ctx->mute = _alsa_get_mute (num); + ctx->idx = num; + ctx->name = strdup (name); + + fprintf (stderr, "Successfully initialized ALSA backend.\n"); + + return 1; +} + +void _alsa_free (const char *name) { + + int num; + + for (num = 0; num < (sizeof(dev_ctx_a)/sizeof(dev_ctx_alsa_T*)); num++) { + if (dev_ctx_a[num]->name && + !strcmp (dev_ctx_a[num]->name, name)) { + snd_pcm_close (dev_ctx_a[num]->dev); + snd_pcm_hw_params_free (dev_ctx_a[num]->params); + free (dev_ctx_a[num]->name); + dev_ctx_a[num]->dev = NULL; + dev_ctx_a[num]->name = NULL; + free (dev_ctx_a[num]); + return; + } + } +} + +void _alsa_play (int num) { + + if (!dev_ctx_a || !dev_ctx_a[num] || dev_ctx_a[num]->thr_should_run || + access (AUDIO_BUFFER, F_OK) == -1) + return; + + dev_ctx_a[num]->thr_should_run = 1; + dev_ctx_a[num]->thr_finished = 0; + pthread_create (&dev_ctx_a[num]->thr, NULL, _alsa_play_thread_fn, (void*)dev_ctx_a[num]); +} + +void _alsa_stop (int num) { + + if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->thr_should_run) + return; + + /* stop the "while" loop in thread */ + dev_ctx_a[num]->thr_should_run = 0; + + while (!dev_ctx_a[num]->thr_finished) + usleep(100000); + + pthread_join (dev_ctx_a[num]->thr, NULL); +} + +unsigned int _alsa_get_volume (int num, unsigned int channel) { + + if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm) + return 0; + + snd_mixer_selem_get_playback_volume (dev_ctx_a[num]->mixer_elm, SCHANNELS[channel], &dev_ctx_a[num]->vol); + + return (unsigned int)(dev_ctx_a[num]->vol*100)/dev_ctx_a[num]->vol_max; +} + +void _alsa_set_volume (int num, unsigned int channel, unsigned int vol) { + + if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm || + vol > 100) + return; + + snd_mixer_selem_set_playback_volume (dev_ctx_a[num]->mixer_elm, SCHANNELS[channel], (vol*dev_ctx_a[num]->vol_max)/100); +} + +void _alsa_set_volume_all (int num, unsigned int vol) { + + if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm || + vol > 100) + + fflush (stdout); /* seems to force this logic to apply quickly */ + snd_mixer_selem_set_playback_volume_all (dev_ctx_a[num]->mixer_elm, (vol*dev_ctx_a[num]->vol_max)/100); +} + +unsigned char _alsa_get_mute (int num) { + + int mute = 0; + snd_mixer_elem_t *elm_m; + + if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm) + return 0; + + dev_ctx_a[num]->mixer_elm_m ? (elm_m = dev_ctx_a[num]->mixer_elm_m) : + (elm_m = dev_ctx_a[num]->mixer_elm); + + if (snd_mixer_selem_has_playback_switch (elm_m)) { + snd_mixer_selem_get_playback_switch (elm_m, SND_MIXER_SCHN_FRONT_LEFT, &mute); + snd_mixer_selem_get_playback_switch (elm_m, SND_MIXER_SCHN_FRONT_RIGHT, &mute); + } + + if (dev_ctx_a[num]->mixer_elm_m) + return (unsigned char)mute; + else + return (unsigned char)!mute; +} + +void _alsa_set_mute (int num, unsigned char tomute) { + + snd_mixer_elem_t *elm_m; + int mute; + + if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm || 1 < tomute) + return; + + if (dev_ctx_a[num]->mixer_elm_m) { + elm_m = dev_ctx_a[num]->mixer_elm_m; + mute = (int)!tomute; + } else { + elm_m = dev_ctx_a[num]->mixer_elm; + mute = (int)tomute; + } + + if (snd_mixer_selem_has_playback_switch (elm_m)) + snd_mixer_selem_set_playback_switch_all (elm_m, !mute); +} + +void _alsa_set_rate (int num, unsigned int rate) { + + if (!dev_ctx_a || !dev_ctx_a[num]) + return; + + snd_pcm_hw_params_set_rate_near (dev_ctx_a[num]->dev, dev_ctx_a[num]->params, &rate, 0); +} + +void _alsa_set_channels (int num, unsigned int channels) { + + if (!dev_ctx_a || !dev_ctx_a[num]) + return; + + snd_pcm_hw_params_set_channels (dev_ctx_a[num]->dev, dev_ctx_a[num]->params, channels); +} + + /* ---- LOCAL THREADED FUNCTIONS ---- */ + +void* _alsa_play_thread_fn (void *ctx) { + + dev_ctx_alsa_T *dev_ctx_a = (dev_ctx_alsa_T *)ctx; + FILE *file = NULL; + char *buf = NULL; + long size; + int frames, res; + + file = fopen (AUDIO_BUFFER, "rb"); + + while (dev_ctx_a->thr_should_run && file && (access (AUDIO_BUFFER, F_OK) != -1) ) { + fseek (file, 0, SEEK_END); + size = ftell (file); + buf = (char*) realloc (buf, size * sizeof(char)); + frames = (size * sizeof(char)) / 4; + + fseek (file, 0, SEEK_SET); + fread (buf, 1, size, file); + fflush (file); + + if ((res = snd_pcm_writei (dev_ctx_a->dev, buf, frames)) != frames) { + snd_pcm_recover (dev_ctx_a->dev, res, 0); + snd_pcm_prepare (dev_ctx_a->dev); + } + /* snd_pcm_drain (dev_ctx->dev); */ + } + if (buf) free(buf); + if (file) fclose(file); + + dev_ctx_a->thr_finished = 1; + return 0; +} diff --git a/bindings/audio/audio-alsa.h b/bindings/audio/audio-alsa.h new file mode 100644 index 00000000..679d4eee --- /dev/null +++ b/bindings/audio/audio-alsa.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef AUDIO_ALSA_H +#define AUDIO_ALSA_H + +#include <pthread.h> +#include <alsa/asoundlib.h> + +#include "audio-api.h" + +#define AUDIO_BUFFER "/tmp/audio_buf" + +typedef struct dev_ctx_alsa dev_ctx_alsa_T; + +struct dev_ctx_alsa { + char *name; + snd_pcm_t *dev; + snd_pcm_hw_params_t *params; + snd_mixer_elem_t *mixer_elm; + snd_mixer_elem_t *mixer_elm_m; + long vol_max; + long vol; + pthread_t thr; + unsigned char thr_should_run; + unsigned char thr_finished; +}; + +unsigned char _alsa_init (const char *, audioCtxHandleT *); +void _alsa_free (const char *); +void _alsa_play (int); +void _alsa_stop (int); +unsigned int _alsa_get_volume (int, unsigned int); +void _alsa_set_volume (int, unsigned int, unsigned int); +void _alsa_set_volume_all (int, unsigned int); +unsigned char _alsa_get_mute (int); +void _alsa_set_mute (int, unsigned char); +void _alsa_set_channels (int, unsigned int); + +void* _alsa_play_thread_fn (void *); + +#endif /* AUDIO_ALSA_H */ diff --git a/bindings/audio/audio-api.c b/bindings/audio/audio-api.c new file mode 100644 index 00000000..1ba9126c --- /dev/null +++ b/bindings/audio/audio-api.c @@ -0,0 +1,383 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#include <stdlib.h> +#include <json-c/json.h> + +#include "audio-api.h" +#include "audio-alsa.h" +#ifdef HAVE_PULSE +#include "audio-pulse.h" +#endif + +#include <afb/afb-plugin.h> +#include <afb/afb-req-itf.h> + +/* ------ BACKEND FUNCTIONS ------- */ + +unsigned char _backend_init (const char *name, audioCtxHandleT *ctx) { + + char *backend_env = getenv ("AFB_AUDIO_OUTPUT"); + unsigned char res = 0; + +# ifdef HAVE_PULSE + if (!backend_env || (strcasecmp (backend_env, "Pulse") == 0)) + res = _pulse_init (name, ctx); + if (!res) +#endif + res = _alsa_init (name, ctx); + + if (!res) + fprintf (stderr, "Could not initialize Audio backend\n"); + + return res; +} + +void _backend_free (audioCtxHandleT *ctx) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) _pulse_free (ctx); else +# endif + _alsa_free (ctx->name); +} + +void _backend_play (audioCtxHandleT *ctx) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) _pulse_play (ctx); else +# endif + _alsa_play (ctx->idx); +} + +void _backend_stop (audioCtxHandleT *ctx) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) _pulse_stop (ctx); else +# endif + _alsa_stop (ctx->idx); +} + +unsigned int _backend_get_volume (audioCtxHandleT *ctx, unsigned int channel) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) return _pulse_get_volume (ctx, channel); else +# endif + return _alsa_get_volume (ctx->idx, channel); +} + +void _backend_set_volume (audioCtxHandleT *ctx, unsigned int channel, unsigned int vol) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) _pulse_set_volume (ctx, channel, vol); else +# endif + _alsa_set_volume (ctx->idx, channel, vol); +} + +void _backend_set_volume_all (audioCtxHandleT *ctx, unsigned int vol) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) _pulse_set_volume_all (ctx, vol); else +# endif + _alsa_set_volume_all (ctx->idx, vol); +} + +unsigned char _backend_get_mute (audioCtxHandleT *ctx) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) return _pulse_get_mute (ctx); else +# endif + return _alsa_get_mute (ctx->idx); +} + +void _backend_set_mute (audioCtxHandleT *ctx, unsigned char mute) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) _pulse_set_mute (ctx, mute); else +# endif + _alsa_set_mute (ctx->idx, mute); +} + +void _backend_set_channels (audioCtxHandleT *ctx, unsigned int channels) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) return; else +# endif + _alsa_set_channels (ctx->idx, channels); +} + +/* ------ LOCAL HELPER FUNCTIONS --------- */ + +/* private client context constructor ; default values */ +static audioCtxHandleT* initAudioCtx () { + + audioCtxHandleT *ctx; + int i; + + ctx = malloc (sizeof(audioCtxHandleT)); + ctx->audio_dev = NULL; + ctx->name = NULL; + ctx->idx = -1; + for (i = 0; i < 8; i++) + ctx->volume[i] = 25; + ctx->channels = 2; + ctx->mute = 0; + ctx->is_playing = 0; + + return ctx; +} + +static void releaseAudioCtx (void *context) { + + audioCtxHandleT *ctx = (audioCtxHandleT*) context; + + /* power it off */ + _backend_free (ctx); + + /* clean client context */ + ctx->audio_dev = NULL; + if (ctx->name) + free (ctx->name); + ctx->idx = -1; + free (ctx); +} + + +/* ------ PUBLIC PLUGIN FUNCTIONS --------- */ + +static void init (struct afb_req request) { /* AFB_SESSION_CHECK */ + + audioCtxHandleT *ctx = afb_req_context_get (request); + json_object *jresp; + + /* create a private client context */ + if (!ctx) { + ctx = initAudioCtx(); + afb_req_context_set (request, ctx, releaseAudioCtx); + } + + if (!_backend_init ("default", ctx)) + afb_req_fail (request, "failed", "backend initialization failed"); + + jresp = json_object_new_object(); + json_object_object_add (jresp, "init", json_object_new_string ("success")); + afb_req_success (request, jresp, "Audio initialized"); +} + +static void volume (struct afb_req request) { /* AFB_SESSION_CHECK */ + + audioCtxHandleT *ctx = afb_req_context_get (request); + const char *value = afb_req_value (request, "value"); + json_object *jresp; + unsigned int volume[8], i; + char *volume_i; + char volume_str[256]; + size_t len_str = 0; + + if (!ctx) { + afb_req_fail (request, "failed", "you must call 'init' first"); + return; + } + jresp = json_object_new_object(); + + /* no "?value=" parameter : return current state */ + if (!value) { + for (i = 0; i < 8; i++) { + ctx->volume[i] = _backend_get_volume (ctx, i); + snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]); + len_str = strlen (volume_str); + } + json_object_object_add (jresp, "volume", json_object_new_string(volume_str)); + afb_req_success (request, jresp, "Audio - Volume obtained"); + return; + } + + /* "?value=" parameter, set volume */ + else { + volume_i = strdup (value); + volume_i = strtok (volume_i, ","); + volume[0] = (unsigned int) atoi (volume_i); + + if (100 < volume[0]) { + free (volume_i); + afb_req_fail (request, "failed", "volume must be between 0 and 100"); + return; + } + ctx->volume[0] = volume[0]; + _backend_set_volume (ctx, 0, ctx->volume[0]); + snprintf (volume_str, sizeof(volume_str), "%d,", ctx->volume[0]); + + for (i = 1; i < 8; i++) { + volume_i = strtok (NULL, ","); + /* if there is only one value, set all channels to this one */ + if (!volume_i && i == 1) + _backend_set_volume_all (ctx, ctx->volume[0]); + if (!volume_i || 100 < atoi(volume_i) || atoi(volume_i) < 0) + ctx->volume[i] = _backend_get_volume (ctx, i); + else { + ctx->volume[i] = (unsigned int) atoi(volume_i); + _backend_set_volume (ctx, i, ctx->volume[i]); + } + len_str = strlen(volume_str); + snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]); + } + free (volume_i); + json_object_object_add (jresp, "volume", json_object_new_string(volume_str)); + } + + afb_req_success (request, jresp, "Audio - Volume changed"); +} + +static void channels (struct afb_req request) { /* AFB_SESSION_CHECK */ + + audioCtxHandleT *ctx = afb_req_context_get (request); + const char *value = afb_req_value (request, "value"); + json_object *jresp; + char channels_str[256]; + + if (!ctx) { + afb_req_fail (request, "failed", "you must call 'init' first"); + return; + } + jresp = json_object_new_object(); + + /* no "?value=" parameter : return current state */ + if (!value) { + snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels); + + json_object_object_add (jresp, "channels", json_object_new_string (channels_str)); + afb_req_success (request, jresp, "Audio - Channels obtained"); + return; + } + + /* "?value=" parameter, set channels */ + else { + ctx->channels = (unsigned int) atoi (value); + _backend_set_channels (ctx, ctx->channels); + snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels); + + jresp = json_object_new_object(); + json_object_object_add (jresp, "channels", json_object_new_string (channels_str)); + } + + afb_req_success (request, jresp, "Audio - Channels set"); +} + +static void mute (struct afb_req request) { /* AFB_SESSION_CHECK */ + + audioCtxHandleT *ctx = afb_req_context_get (request); + const char *value = afb_req_value (request, "value"); + json_object *jresp; + + if (!ctx) { + afb_req_fail (request, "failed", "you must call 'init' first"); + return; + } + jresp = json_object_new_object(); + + /* no "?value=" parameter : return current state */ + if (!value) { + ctx->mute = _backend_get_mute (ctx); + ctx->mute ? + json_object_object_add (jresp, "mute", json_object_new_string ("on")) + : json_object_object_add (jresp, "mute", json_object_new_string ("off")); + afb_req_success (request, jresp, "Audio - Mute status obtained"); + return; + } + + /* "?value=" parameter is "1" or "true" */ + else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) { + ctx->mute = 1; + _backend_set_mute (ctx, ctx->mute); + json_object_object_add (jresp, "mute", json_object_new_string ("on")); + } + + /* "?value=" parameter is "0" or "false" */ + else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) { + ctx->mute = 0; + _backend_set_mute (ctx, ctx->mute); + json_object_object_add (jresp, "mute", json_object_new_string ("off")); + } + + afb_req_success (request, jresp, "Audio - Mute set"); +} + +static void play (struct afb_req request) { /* AFB_SESSION_CHECK */ + + audioCtxHandleT *ctx = afb_req_context_get (request); + const char *value = afb_req_value (request, "value"); + json_object *jresp; + + if (!ctx) { + afb_req_fail (request, "failed", "you must call 'init' first"); + return; + } + jresp = json_object_new_object(); + + /* no "?value=" parameter : return current state */ + if (!value) { + ctx->is_playing ? + json_object_object_add (jresp, "play", json_object_new_string ("on")) + : json_object_object_add (jresp, "play", json_object_new_string ("off")); + afb_req_success (request, jresp, "Audio - Playing status obtained"); + return; + } + + /* "?value=" parameter is "1" or "true" */ + else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) { + ctx->is_playing = 1; + _backend_play (ctx); + json_object_object_add (jresp, "play", json_object_new_string ("on")); + } + + /* "?value=" parameter is "0" or "false" */ + else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) { + ctx->is_playing = 0; + _backend_stop (ctx); + json_object_object_add (jresp, "play", json_object_new_string ("off")); + } + + afb_req_success (request, jresp, "Audio - Play"); +} + +static void ping (struct afb_req request) { /* AFB_SESSION_NONE */ + afb_req_success (request, NULL, "Audio - Ping success"); +} + +static const struct AFB_verb_desc_v1 verbs[] = { + {"init" , AFB_SESSION_CHECK, init , "Audio API - init"}, + {"volume" , AFB_SESSION_CHECK, volume , "Audio API - volume"}, + {"channels", AFB_SESSION_CHECK, channels , "Audio API - channels"}, + {"mute" , AFB_SESSION_CHECK, mute , "Audio API - mute"}, + {"play" , AFB_SESSION_CHECK, play , "Audio API - play"}, + {"ping" , AFB_SESSION_NONE, ping , "Audio API - ping"}, + {NULL} +}; + +static const struct AFB_plugin pluginDesc = { + .type = AFB_PLUGIN_VERSION_1, + .v1 = { + .info = "Application Framework Binder - Audio plugin", + .prefix = "audio", + .verbs = verbs + } +}; + +const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *itf) +{ + return &pluginDesc; +} diff --git a/bindings/audio/audio-api.h b/bindings/audio/audio-api.h new file mode 100644 index 00000000..f5c77c2e --- /dev/null +++ b/bindings/audio/audio-api.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef AUDIO_API_H +#define AUDIO_API_H + +/* global plugin handle, should store everything we may need */ +typedef struct { + int devCount; +} pluginHandleT; + +/* private client context [will be destroyed when client leaves] */ +typedef struct { + void *audio_dev; /* handle to implementation (ALSA, PulseAudio...) */ + char *name; /* name of the audio card */ + int idx; /* audio card index within global array */ + unsigned int volume[8]; /* audio volume (8 channels) : 0-100 */ + unsigned int channels; /* audio channels : 1(mono)/2(stereo)... */ + unsigned char mute; /* audio muted : 0(false)/1(true) */ + unsigned char is_playing; /* audio is playing: 0(false)/1(true) */ +} audioCtxHandleT; + + +#endif /* AUDIO_API_H */ diff --git a/bindings/audio/audio-pulse.c b/bindings/audio/audio-pulse.c new file mode 100644 index 00000000..34643603 --- /dev/null +++ b/bindings/audio/audio-pulse.c @@ -0,0 +1,491 @@ +/* + * Copyright (C) 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#include <stdio.h> + +#include "audio-api.h" +#include "audio-pulse.h" + +static struct alsa_info **alsa_info = NULL; +static struct dev_ctx_pulse **dev_ctx_p = NULL; +static unsigned int client_count = 0; + + +unsigned char _pulse_init (const char *name, audioCtxHandleT *ctx) { + + pa_mainloop *pa_loop; + pa_mainloop_api *pa_api; + pa_context *pa_context; + pa_simple *pa; + pa_sample_spec *pa_spec; + struct timeval tv_start, tv_now; + int ret, error, i; + + pa_loop = pa_mainloop_new (); + pa_api = pa_mainloop_get_api (pa_loop); + pa_context = pa_context_new (pa_api, "afb-audio-plugin"); + + /* allocate the global array if it hasn't been done */ + if (!dev_ctx_p) + dev_ctx_p = (dev_ctx_pulse_T**) malloc (sizeof(dev_ctx_pulse_T*)); + + /* create a temporary device, to be held until sink gets discovered */ + dev_ctx_pulse_T *dev_ctx_p_t = (dev_ctx_pulse_T*) malloc (sizeof(dev_ctx_pulse_T)); + dev_ctx_p_t->sink_name = NULL; + dev_ctx_p_t->card_name = (char**) malloc (sizeof(char*)); + dev_ctx_p_t->card_name[0] = strdup (name); + dev_ctx_p_t->pa_loop = pa_loop; + dev_ctx_p_t->pa_context = pa_context; + + pa_context_set_state_callback (pa_context, _pulse_context_cb, (void*)dev_ctx_p_t); + pa_context_connect (pa_context, NULL, 0, NULL); + + /* 1 second should be sufficient to retrieve sink info */ + gettimeofday (&tv_start, NULL); + gettimeofday (&tv_now, NULL); + while (tv_now.tv_sec - tv_start.tv_sec <= 2) { + pa_mainloop_iterate (pa_loop, 0, &ret); + + if (ret == -1) { /* generic error */ + fprintf (stderr, "Stopping PulseAudio backend...\n"); + return 0; + } + + if ((ret > 0)&&(ret < 100)) { /* 0 and >100 are PulseAudio codes */ + /* found a matching sink from callback */ + fprintf (stderr, "Success : using sink n.%d\n", ret-1); + ctx->audio_dev = (void*)dev_ctx_p[ret-1]; + break; + } + gettimeofday (&tv_now, NULL); + } + /* fail if we found no matching sink */ + if (!ctx->audio_dev) + return 0; + + /* make the client context aware of current card state */ + ctx->mute = (unsigned char)dev_ctx_p[ret-1]->mute; + ctx->channels = (unsigned int)dev_ctx_p[ret-1]->volume.channels; + for (i = 0; i < ctx->channels; i++) + ctx->volume[i] = dev_ctx_p[ret-1]->volume.values[i]; + ctx->idx = ret-1; + + /* open matching sink for playback */ + pa_spec = (pa_sample_spec*) malloc (sizeof(pa_sample_spec)); + pa_spec->format = PA_SAMPLE_S16LE; + pa_spec->rate = 22050; + pa_spec->channels = (uint8_t)ctx->channels; + + if (!(pa = pa_simple_new (NULL, "afb-audio-plugin", PA_STREAM_PLAYBACK, dev_ctx_p[ret-1]->sink_name, + "afb-audio-output", pa_spec, NULL, NULL, &error))) { + fprintf (stderr, "Error opening PulseAudio sink %s : %s\n", + dev_ctx_p[ret-1]->sink_name, pa_strerror(error)); + return 0; + } + dev_ctx_p[ret-1]->pa = pa; + free (pa_spec); + + client_count++; + + fprintf (stderr, "Successfully initialized PulseAudio backend.\n"); + + return 1; +} + +void _pulse_free (audioCtxHandleT *ctx) { + + int num, i; + + client_count--; + if (client_count > 0) return; + + for (num = 0; num < (sizeof(dev_ctx_p)/sizeof(dev_ctx_pulse_T*)); num++) { + + for (i = 0; num < (sizeof(dev_ctx_p[num]->card_name)/sizeof(char*)); i++) { + free (dev_ctx_p[num]->card_name[i]); + dev_ctx_p[num]->card_name[i] = NULL; + } + pa_context_disconnect (dev_ctx_p[num]->pa_context); + pa_context_unref (dev_ctx_p[num]->pa_context); + pa_mainloop_free (dev_ctx_p[num]->pa_loop); + pa_simple_free (dev_ctx_p[num]->pa); + free (dev_ctx_p[num]->sink_name); + dev_ctx_p[num]->pa_context = NULL; + dev_ctx_p[num]->pa_loop = NULL; + dev_ctx_p[num]->pa = NULL; + dev_ctx_p[num]->sink_name = NULL; + free (dev_ctx_p[num]); + } +} + +void _pulse_play (audioCtxHandleT *ctx) { + + dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev; + + if (!dev_ctx_p_c || dev_ctx_p_c->thr_should_run || access (AUDIO_BUFFER, F_OK) == -1) + return; + + dev_ctx_p_c->thr_should_run = 1; + dev_ctx_p_c->thr_finished = 0; + pthread_create (&dev_ctx_p_c->thr, NULL, _pulse_play_thread_fn, (void*)dev_ctx_p_c); +} + +void _pulse_stop (audioCtxHandleT *ctx) { + + dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev; + + if (!dev_ctx_p_c || !dev_ctx_p_c->thr_should_run) + return; + + dev_ctx_p_c->thr_should_run = 0; + while (!dev_ctx_p_c->thr_finished) + usleep(100000); + pthread_join (dev_ctx_p_c->thr, NULL); +} + +unsigned int _pulse_get_volume (audioCtxHandleT *ctx, unsigned int channel) { + + dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev; + + if (!dev_ctx_p_c) + return 0; + + _pulse_refresh_sink (dev_ctx_p_c); + + return (dev_ctx_p_c->volume.values[channel]*100)/PA_VOLUME_NORM; +} + +void _pulse_set_volume (audioCtxHandleT *ctx, unsigned int channel, unsigned int vol) { + + dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev; + struct pa_cvolume volume; + + if (!dev_ctx_p_c) + return; + + volume = dev_ctx_p_c->volume; + volume.values[channel] = (vol*PA_VOLUME_NORM)/100; + + pa_context_set_sink_volume_by_name (dev_ctx_p_c->pa_context, dev_ctx_p_c->sink_name, + &volume, NULL, NULL); + _pulse_refresh_sink (dev_ctx_p_c); +} + +void _pulse_set_volume_all (audioCtxHandleT *ctx, unsigned int vol) { + + dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev; + struct pa_cvolume volume; + + if (!dev_ctx_p_c) + return; + + pa_cvolume_init (&volume); + pa_cvolume_set (&volume, dev_ctx_p_c->volume.channels, vol); + + pa_context_set_sink_volume_by_name (dev_ctx_p_c->pa_context, dev_ctx_p_c->sink_name, + &volume, NULL, NULL); + _pulse_refresh_sink (dev_ctx_p_c); +} + +unsigned char _pulse_get_mute (audioCtxHandleT *ctx) { + + dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev; + + if (!dev_ctx_p_c) + return 0; + + _pulse_refresh_sink (dev_ctx_p_c); + + return (unsigned char)dev_ctx_p_c->mute; +} + +void _pulse_set_mute (audioCtxHandleT *ctx, unsigned char mute) { + + dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev; + + if (!dev_ctx_p_c) + return; + + pa_context_set_sink_mute_by_name (dev_ctx_p_c->pa_context, dev_ctx_p_c->sink_name, + (int)mute, NULL, NULL); + _pulse_refresh_sink (dev_ctx_p_c); +} + + /* ---- LOCAL HELPER FUNCTIONS ---- */ + +void _pulse_refresh_sink (dev_ctx_pulse_T* dev_ctx_p_c) { + + pa_mainloop_api *pa_api; + + dev_ctx_p_c->pa_loop = pa_mainloop_new (); + pa_api = pa_mainloop_get_api (dev_ctx_p_c->pa_loop); + dev_ctx_p_c->pa_context = pa_context_new (pa_api, "afb-audio-plugin"); + + dev_ctx_p_c->refresh = 1; + + switch (pa_context_get_state (dev_ctx_p_c->pa_context)) { + case PA_CONTEXT_READY: + pa_context_get_sink_info_by_name (dev_ctx_p_c->pa_context, + dev_ctx_p_c->sink_name, + _pulse_sink_info_cb, (void*)dev_ctx_p_c); + break; + default: + return; + } + + while (dev_ctx_p_c->refresh) + pa_mainloop_iterate (dev_ctx_p_c->pa_loop, 0, NULL); +} + +void _pulse_enumerate_cards () { + + void **cards, **card; + char *name, *found, *alsa_name, *card_name; + int new_info, i, num = 0; + + /* allocate the global alsa array */ + alsa_info = (alsa_info_T**) malloc (sizeof(alsa_info_T*)); + alsa_info[0] = (alsa_info_T*) malloc (sizeof(alsa_info_T)); + alsa_info[0]->device = NULL; + alsa_info[0]->synonyms = NULL; + + /* we use ALSA to enumerate cards */ + snd_device_name_hint (-1, "pcm", &cards); + card = cards; + + for (; *card != NULL; card++) { + name = snd_device_name_get_hint (*card, "NAME"); + new_info = 1; + + /* alsa name is before ':' (if ':' misses, then it has no card) */ + found = strstr (name, ":"); + if (!found) continue; + /* */ + alsa_name = (char*) malloc (found-name+1); + strncpy (alsa_name, name, found-name); + alsa_name[found-name] = '\0'; + + /* card name is the invariant between "CARD=" and ',' */ + found = strstr (name, "CARD="); + if (!found) continue; + /* */ + found += 5; + card_name = strdup (found); + found = strstr (card_name, ","); + if (found) card_name[found-card_name] = '\0'; + + /* was the card name already listed in the global alsa array ? */ + for (i = 0; i < (sizeof(alsa_info)/sizeof(alsa_info_T*)); i++) { + + if (alsa_info[i]->device && + !strcmp (alsa_info[i]->device, card_name)) { + /* it was ; add the alsa name as a new synonym */ + asprintf (&alsa_info[i]->synonyms, "%s:%s", alsa_info[i]->synonyms, alsa_name); + new_info = 0; + break; + } + } + /* it was not ; create it */ + if (new_info) { + alsa_info = (alsa_info_T**) realloc (alsa_info, (num+1)*sizeof(alsa_info_T*)); + alsa_info[num] = (alsa_info_T*) malloc (sizeof(alsa_info_T)); + alsa_info[num]->device = strdup (card_name); + asprintf (&alsa_info[num]->synonyms, ":%s", alsa_name); + num++; + } + free (alsa_name); + free (card_name); + } +} + +char** _pulse_find_cards (const char *name) { + + char **cards = NULL; + char *needle, *found, *next; + int num, i = 0; + + if (!alsa_info) + _pulse_enumerate_cards (); + + asprintf (&needle, ":%s", name); + + for (num = 0; num < (sizeof(alsa_info)/sizeof(alsa_info_T*)); num++) { + + found = strstr (alsa_info[num]->synonyms, needle); + while (found) { + /* if next character is not ':' or '\0', we are wrong */ + if ((found[strlen(name)+1] != ':') && (found[strlen(name)+1] != '\0')) { + found = strstr (found+1, needle); + continue; + } + /* found it ; now return all the "synonym" cards */ + found = strstr (alsa_info[num]->synonyms, ":"); + while (found) { + next = strstr (found+1, ":"); + if (!next) break; + cards = (char**) realloc (cards, (i+1)*sizeof(char*)); + cards[i] = (char*) malloc (next-found+1); + strncpy (cards[i], found+1, next-found); + cards[i][next-found-1] = '\0'; + found = next; i++; + } + } + } + free (needle); + + return cards; +} + + /* ---- LOCAL CALLBACK FUNCTIONS ---- */ + +void _pulse_context_cb (pa_context *context, void *data) { + + pa_context_state_t state = pa_context_get_state (context); + dev_ctx_pulse_T *dev_ctx_p_t = (dev_ctx_pulse_T *)data; + + if (state == PA_CONTEXT_FAILED) { + fprintf (stderr, "Could not connect to PulseAudio !\n"); + pa_mainloop_quit (dev_ctx_p_t->pa_loop, -1); + pa_context_disconnect (dev_ctx_p_t->pa_context); + pa_context_unref (dev_ctx_p_t->pa_context); + pa_mainloop_free (dev_ctx_p_t->pa_loop); + } + + if (state == PA_CONTEXT_READY) + pa_context_get_sink_info_list (context, _pulse_sink_list_cb, (void*)dev_ctx_p_t); +} + +void _pulse_sink_list_cb (pa_context *context, const pa_sink_info *info, + int eol, void *data) { + + dev_ctx_pulse_T *dev_ctx_p_t = (dev_ctx_pulse_T *)data; + const char *device_string; + char *device, *found; + char **cards; + int num, i; + + if (eol != 0) + return; + + device_string = pa_proplist_gets (info->proplist, "device.string"); + /* ignore sinks with no cards */ + if (!device_string) + return; + + /* was a sink with similar name already found ? */ + for (num = 0; num < (sizeof(dev_ctx_p)/sizeof(dev_ctx_pulse_T*)); num++) { + if (dev_ctx_p[num]->sink_name && + !strcmp (dev_ctx_p[num]->sink_name, info->name)) { + + /* yet it was, did it have the required card ? */ + cards = dev_ctx_p[num]->card_name; + for (i = 0; i < (sizeof(cards)/sizeof(char*)); i++) { + if (!strcmp (cards[i], dev_ctx_p_t->card_name[0])) { + /* it did : stop there and succeed */ + fprintf (stderr, "Found matching sink : %s\n", info->name); + /* we return num+1 because '0' is already used */ + pa_mainloop_quit (dev_ctx_p_t->pa_loop, num+1); + pa_context_disconnect (dev_ctx_p_t->pa_context); + pa_context_unref (dev_ctx_p_t->pa_context); + pa_mainloop_free (dev_ctx_p_t->pa_loop); + } + } + /* it did not, ignore and return */ + return; + } + } + num++; + + /* remove ending ":0",":1"... in device name */ + device = strdup (device_string); + found = strstr (device, ":"); + if (found) device[found-device] = '\0'; + + /* new sink, find all the cards it manages, fail if none */ + cards = _pulse_find_cards (device); + free (device); + if (!cards) + return; + + /* everything is well, register it in global array */ + dev_ctx_p_t->sink_name = strdup (info->name); + dev_ctx_p_t->card_name = cards; + dev_ctx_p_t->mute = info->mute; + dev_ctx_p_t->volume = info->volume; + dev_ctx_p_t->thr_should_run = 0; + dev_ctx_p_t->thr_finished = 0; + dev_ctx_p[num] = dev_ctx_p_t; + + /* does this new sink have the card we are looking for ? */ /* TODO : factorize this */ + for (i = 0; i < (sizeof(cards)/sizeof(char*)); i++) { + if (!strcmp (cards[i], dev_ctx_p_t->card_name[0])) { + /* it did : stop there and succeed */ + fprintf (stderr, "Found matching sink : %s\n", info->name); + /* we return num+1 because '0' is already used */ + pa_mainloop_quit (dev_ctx_p_t->pa_loop, num+1); + pa_context_disconnect (dev_ctx_p_t->pa_context); + pa_context_unref (dev_ctx_p_t->pa_context); + pa_mainloop_free (dev_ctx_p_t->pa_loop); + } + } +} + +void _pulse_sink_info_cb (pa_context *context, const pa_sink_info *info, + int eol, void *data) { + + dev_ctx_pulse_T *dev_ctx_p_c = (dev_ctx_pulse_T *)data; + + if (eol != 0) + return; + + dev_ctx_p_c->refresh = 0; + dev_ctx_p_c->mute = info->mute; + dev_ctx_p_c->volume = info->volume; +} + + /* ---- LOCAL THREADED FUNCTIONS ---- */ + +void* _pulse_play_thread_fn (void *ctx) { + + dev_ctx_pulse_T *dev_ctx_p_c = (dev_ctx_pulse_T *)ctx; + FILE *file = NULL; + char *buf = NULL; + long size; + int error; + + file = fopen (AUDIO_BUFFER, "rb"); + + while (dev_ctx_p_c->thr_should_run && file && (access (AUDIO_BUFFER, F_OK) != -1) ) { + fseek (file, 0, SEEK_END); + size = ftell (file); + buf = (char*) realloc (buf, size * sizeof(char)); + + fseek (file, 0, SEEK_SET); + fread (buf, 1, size, file); + fflush (file); + + if (pa_simple_write (dev_ctx_p_c->pa, buf, size*2, &error) < 0) + fprintf (stderr, "Error writing to PulseAudio : %s\n", pa_strerror (error)); + /* pa_simple_drain (dev_ctx_p_c->pa); */ + } + if (buf) free(buf); + if (file) fclose(file); + + dev_ctx_p_c->thr_finished = 1; + return 0; +} diff --git a/bindings/audio/audio-pulse.h b/bindings/audio/audio-pulse.h new file mode 100644 index 00000000..ad8ff490 --- /dev/null +++ b/bindings/audio/audio-pulse.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef AUDIO_PULSE_H +#define AUDIO_PULSE_H + +#include <sys/time.h> +#include <pulse/pulseaudio.h> +#include <pulse/simple.h> +#include <pulse/error.h> + +#include "audio-alsa.h" + +typedef struct dev_ctx_pulse dev_ctx_pulse_T; +typedef struct alsa_info alsa_info_T; + +struct dev_ctx_pulse { + char *sink_name; + char **card_name; + pa_mainloop *pa_loop; + pa_context *pa_context; + pa_simple *pa; + pa_cvolume volume; + int mute; + unsigned char refresh; + pthread_t thr; + unsigned char thr_should_run; + unsigned char thr_finished; +}; + +struct alsa_info { + char *device; + char *synonyms; +}; + +unsigned char _pulse_init (const char *, audioCtxHandleT *); +void _pulse_free (audioCtxHandleT *); +void _pulse_play (audioCtxHandleT *); +void _pulse_stop (audioCtxHandleT *); +unsigned int _pulse_get_volume (audioCtxHandleT *, unsigned int); +void _pulse_set_volume (audioCtxHandleT *, unsigned int, unsigned int); +void _pulse_set_volume_all (audioCtxHandleT *, unsigned int); +unsigned char _pulse_get_mute (audioCtxHandleT *); +void _pulse_set_mute (audioCtxHandleT *, unsigned char); + +void _pulse_context_cb (pa_context *, void *); +void _pulse_sink_list_cb (pa_context *, const pa_sink_info *, int, void *); +void _pulse_sink_info_cb (pa_context *, const pa_sink_info *, int, void *); +void* _pulse_play_thread_fn (void *); +void _pulse_refresh_sink (dev_ctx_pulse_T *); + +#endif /* AUDIO_PULSE_H */ diff --git a/bindings/audio/export.map b/bindings/audio/export.map new file mode 100644 index 00000000..0ef1ac79 --- /dev/null +++ b/bindings/audio/export.map @@ -0,0 +1 @@ +{ global: afbBindingV1Register; local: *; }; diff --git a/bindings/intrinsics/CMakeLists.txt b/bindings/intrinsics/CMakeLists.txt new file mode 100644 index 00000000..c6ff8ef4 --- /dev/null +++ b/bindings/intrinsics/CMakeLists.txt @@ -0,0 +1,15 @@ + +INCLUDE_DIRECTORIES(${include_dirs}) + +################################################## +# DBus Binding +################################################## +ADD_LIBRARY(afb-dbus-binding MODULE afb-dbus-binding.c) +SET_TARGET_PROPERTIES(afb-dbus-binding PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" +) +TARGET_LINK_LIBRARIES(afb-dbus-binding ${link_libraries}) +INSTALL(TARGETS afb-dbus-binding + LIBRARY DESTINATION ${binding_install_dir}) + diff --git a/bindings/intrinsics/afb-dbus-binding.c b/bindings/intrinsics/afb-dbus-binding.c new file mode 100644 index 00000000..59fe78d6 --- /dev/null +++ b/bindings/intrinsics/afb-dbus-binding.c @@ -0,0 +1,631 @@ +/* + * Copyright (C) 2016 "IoT.bzh" + * Author José Bollo <jose.bollo@iot.bzh> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#include <stdio.h> +#include <string.h> +#include <json-c/json.h> + +#include <systemd/sd-bus.h> +#include <systemd/sd-bus-protocol.h> + +#include <afb/afb-binding.h> + +/* + * the interface to afb-daemon + */ +const struct afb_binding_interface *afbitf; + +/* + * union of possible dbus values + */ +union any { + uint8_t u8; + int16_t i16; + uint16_t u16; + int32_t i32; + uint32_t u32; + int64_t i64; + uint64_t u64; + double dbl; + const char *cstr; + char *str; +}; + +static int unpacklist(struct sd_bus_message *msg, struct json_object **result); +static int packlist(struct sd_bus_message *msg, const char *signature, struct json_object *list); + +/* + * Get the string of 'key' from 'obj' + * Returns NULL if 'key' isn't in 'obj' + */ +static const char *strval(struct json_object *obj, const char *key) +{ + struct json_object *keyval; + return json_object_object_get_ex(obj, key, &keyval) ? json_object_get_string(keyval) : NULL; +} + +/* + * Signature of a json object + */ +static const char *signature_for_json(struct json_object *obj) +{ + switch (json_object_get_type(obj)) { + default: + case json_type_null: + return NULL; + case json_type_boolean: + return "b"; + case json_type_double: + return "d"; + case json_type_int: + return "i"; + case json_type_object: + return "a{sv}"; + case json_type_array: + return "av"; + case json_type_string: + return "s"; + } +} + +/* + * Length of a single complete type + */ +static int lentype(const char *signature, int allows_dict, int allows_not_basic) +{ + int rc, len; + switch(signature[0]) { + + case SD_BUS_TYPE_ARRAY: + if (!allows_not_basic) + break; + rc = lentype(signature + 1, 1, 1); + if (rc < 0) + break; + return 1 + rc; + + case SD_BUS_TYPE_STRUCT_BEGIN: + if (!allows_not_basic) + break; + len = 1; + rc = lentype(signature + len, 0, 1); + while (rc > 0 && signature[len] != SD_BUS_TYPE_STRUCT_END) { + len += rc; + rc = lentype(signature + len, 0, 1); + } + if (rc < 0) + break; + return 1 + len; + + case SD_BUS_TYPE_DICT_ENTRY_BEGIN: + if (!allows_not_basic || !allows_dict) + break; + rc = lentype(signature + 1, 0, 0); + if (rc < 0) + break; + len = 1 + rc; + rc = lentype(signature + len, 0, 1); + if (rc < 0 || signature[len + rc] != SD_BUS_TYPE_DICT_ENTRY_END) + break; + return len + rc + 1; + + case '\x0': + case SD_BUS_TYPE_STRUCT: + case SD_BUS_TYPE_STRUCT_END: + case SD_BUS_TYPE_DICT_ENTRY: + case SD_BUS_TYPE_DICT_ENTRY_END: + break; + + default: + return 1; + } + return -1; +} + + +/* + * Unpack a D-Bus message to a json object + */ +static int unpacksingle(struct sd_bus_message *msg, struct json_object **result) +{ + char c; + int rc; + union any any; + const char *content; + struct json_object *item; + + *result = NULL; + rc = sd_bus_message_peek_type(msg, &c, &content); + if (rc <= 0) + return rc; + + switch (c) { + case SD_BUS_TYPE_BYTE: + case SD_BUS_TYPE_BOOLEAN: + case SD_BUS_TYPE_INT16: + case SD_BUS_TYPE_UINT16: + case SD_BUS_TYPE_INT32: + case SD_BUS_TYPE_UINT32: + case SD_BUS_TYPE_INT64: + case SD_BUS_TYPE_UINT64: + case SD_BUS_TYPE_DOUBLE: + case SD_BUS_TYPE_STRING: + case SD_BUS_TYPE_OBJECT_PATH: + case SD_BUS_TYPE_SIGNATURE: + rc = sd_bus_message_read_basic(msg, c, &any); + if (rc < 0) + goto error; + switch (c) { + case SD_BUS_TYPE_BOOLEAN: + *result = json_object_new_boolean(any.i32); + break; + case SD_BUS_TYPE_BYTE: + *result = json_object_new_int(any.u8); + break; + case SD_BUS_TYPE_INT16: + *result = json_object_new_int(any.i16); + break; + case SD_BUS_TYPE_UINT16: + *result = json_object_new_int(any.u16); + break; + case SD_BUS_TYPE_INT32: + *result = json_object_new_int(any.i32); + break; + case SD_BUS_TYPE_UINT32: + *result = json_object_new_int64(any.u32); + break; + case SD_BUS_TYPE_INT64: + *result = json_object_new_int64(any.i64); + break; + case SD_BUS_TYPE_UINT64: + *result = json_object_new_int64((int64_t)any.u64); + break; + case SD_BUS_TYPE_DOUBLE: + *result = json_object_new_string(any.cstr); + break; + case SD_BUS_TYPE_STRING: + case SD_BUS_TYPE_OBJECT_PATH: + case SD_BUS_TYPE_SIGNATURE: + *result = json_object_new_string(any.cstr); + break; + } + return *result == NULL ? -1 : 1; + + case SD_BUS_TYPE_ARRAY: + case SD_BUS_TYPE_VARIANT: + case SD_BUS_TYPE_STRUCT: + case SD_BUS_TYPE_DICT_ENTRY: + rc = sd_bus_message_enter_container(msg, c, content); + if (rc < 0) + goto error; + if (c == SD_BUS_TYPE_ARRAY && content[0] == SD_BUS_TYPE_DICT_ENTRY_BEGIN && content[1] == SD_BUS_TYPE_STRING) { + *result = json_object_new_object(); + if (*result == NULL) + return -1; + for(;;) { + rc = sd_bus_message_enter_container(msg, 0, NULL); + if (rc < 0) + goto error; + if (rc == 0) + break; + rc = sd_bus_message_read_basic(msg, SD_BUS_TYPE_STRING, &any); + if (rc < 0) + goto error; + rc = unpacksingle(msg, &item); + if (rc < 0) + goto error; + json_object_object_add(*result, any.cstr, item); + rc = sd_bus_message_exit_container(msg); + if (rc < 0) + goto error; + } + } else { + rc = unpacklist(msg, result); + if (rc < 0) + goto error; + } + rc = sd_bus_message_exit_container(msg); + if (rc < 0) + goto error; + return 1; + default: + goto error; + } +error: + json_object_put(*result); + return -1; +} + +/* + * Unpack a D-Bus message to a json object + */ +static int unpacklist(struct sd_bus_message *msg, struct json_object **result) +{ + int rc; + struct json_object *item; + + /* allocates the result */ + *result = json_object_new_array(); + if (*result == NULL) + goto error; + + /* read the values */ + for (;;) { + rc = unpacksingle(msg, &item); + if (rc < 0) + goto error; + if (rc == 0) + return 0; + json_object_array_add(*result, item); + } +error: + json_object_put(*result); + *result = NULL; + return -1; +} + +static int packsingle(struct sd_bus_message *msg, const char *signature, struct json_object *item) +{ + int index, count, rc, len; + union any any; + char *subsig; + struct json_object_iterator it, end; + + len = lentype(signature, 0, 1); + if (len < 0) + goto error; + + switch (*signature) { + case SD_BUS_TYPE_BOOLEAN: + any.i32 = json_object_get_boolean(item); + break; + + case SD_BUS_TYPE_BYTE: + any.i32 = json_object_get_int(item); + if (any.i32 != (int32_t)(uint8_t)any.i32) + goto error; + any.u8 = (uint8_t)any.i32; + break; + + case SD_BUS_TYPE_INT16: + any.i32 = json_object_get_int(item); + if (any.i32 != (int32_t)(int16_t)any.i32) + goto error; + any.i16 = (int16_t)any.i32; + break; + + case SD_BUS_TYPE_UINT16: + any.i32 = json_object_get_int(item); + if (any.i32 != (int32_t)(uint16_t)any.i32) + goto error; + any.u16 = (uint16_t)any.i32; + break; + + case SD_BUS_TYPE_INT32: + any.i64 = json_object_get_int64(item); + if (any.i64 != (int64_t)(int32_t)any.i64) + goto error; + any.i32 = (int32_t)any.i64; + break; + + case SD_BUS_TYPE_UINT32: + any.i64 = json_object_get_int64(item); + if (any.i64 != (int64_t)(uint32_t)any.i64) + goto error; + any.u32 = (uint32_t)any.i64; + break; + + case SD_BUS_TYPE_INT64: + any.i64 = json_object_get_int64(item); + break; + + case SD_BUS_TYPE_UINT64: + any.u64 = (uint64_t)json_object_get_int64(item); + break; + + case SD_BUS_TYPE_DOUBLE: + any.dbl = json_object_get_double(item); + break; + + case SD_BUS_TYPE_STRING: + case SD_BUS_TYPE_OBJECT_PATH: + case SD_BUS_TYPE_SIGNATURE: + any.cstr = json_object_get_string(item); + break; + + case SD_BUS_TYPE_VARIANT: + signature = signature_for_json(item); + if (signature == NULL) + goto error; + rc = sd_bus_message_open_container(msg, SD_BUS_TYPE_VARIANT, signature); + if (rc < 0) + goto error; + rc = packsingle(msg, signature, item); + if (rc < 0) + goto error; + rc = sd_bus_message_close_container(msg); + if (rc < 0) + goto error; + return len; + + case SD_BUS_TYPE_ARRAY: + subsig = strndupa(signature + 1, len - 1); + rc = sd_bus_message_open_container(msg, SD_BUS_TYPE_ARRAY, subsig); + if (rc < 0) + goto error; + if (json_object_is_type(item, json_type_array)) { + /* Is an array! */ + count = json_object_array_length(item); + index = 0; + while(index < count) { + rc = packsingle(msg, subsig, json_object_array_get_idx(item, index++)); + if (rc < 0) + goto error; + } + } else { + /* Not an array! Check if it matches an string dictionnary */ + if (!json_object_is_type(item, json_type_object)) + goto error; + if (*subsig++ != SD_BUS_TYPE_DICT_ENTRY_BEGIN) + goto error; + if (*subsig != SD_BUS_TYPE_STRING) + goto error; + /* iterate the object values */ + subsig[strlen(subsig) - 1] = 0; + it = json_object_iter_begin(item); + end = json_object_iter_end(item); + while (!json_object_iter_equal(&it, &end)) { + rc = sd_bus_message_open_container(msg, SD_BUS_TYPE_DICT_ENTRY, subsig); + if (rc < 0) + goto error; + any.cstr = json_object_iter_peek_name(&it); + rc = sd_bus_message_append_basic(msg, *subsig, &any); + if (rc < 0) + goto error; + rc = packsingle(msg, subsig + 1, json_object_iter_peek_value(&it)); + if (rc < 0) + goto error; + rc = sd_bus_message_close_container(msg); + if (rc < 0) + goto error; + json_object_iter_next(&it); + } + } + rc = sd_bus_message_close_container(msg); + if (rc < 0) + goto error; + return len; + + case SD_BUS_TYPE_STRUCT_BEGIN: + case SD_BUS_TYPE_DICT_ENTRY_BEGIN: + subsig = strndupa(signature + 1, len - 2); + rc = sd_bus_message_open_container(msg, + ((*signature) == SD_BUS_TYPE_STRUCT_BEGIN) ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, + subsig); + if (rc < 0) + goto error; + rc = packlist(msg, subsig, item); + if (rc < 0) + goto error; + rc = sd_bus_message_close_container(msg); + if (rc < 0) + goto error; + return len; + + default: + goto error; + } + + rc = sd_bus_message_append_basic(msg, *signature, &any); + if (rc < 0) + goto error; + return len; + +error: + return -1; +} + +static int packlist(struct sd_bus_message *msg, const char *signature, struct json_object *list) +{ + int rc, count, index, scan; + struct json_object *item; + + scan = 0; + if (list == NULL) { + /* empty case */ + if (*signature) + goto error; + return scan; + } + + if (!json_object_is_type(list, json_type_array)) { + /* down grade gracefully to single */ + rc = packsingle(msg, signature, list); + if (rc < 0) + goto error; + scan = rc; + if (signature[scan] != 0) + goto error; + return scan; + } + + /* iterate over elements */ + count = json_object_array_length(list); + index = 0; + for (;;) { + /* check state */ + if (index == count && signature[scan] == 0) + return scan; + if (index == count || signature[scan] == 0) + goto error; + + /* get the item */ + item = json_object_array_get_idx(list, index); + if (item == NULL) + goto error; + + /* pack the item */ + rc = packsingle(msg, signature + scan, item); + if (rc < 0) + goto error; + + /* advance */ + scan += rc; + index++; + } + +error: + return -(scan + 1); +} + +/* + * handle the reply + */ +static int on_rawcall_reply(sd_bus_message *msg, struct afb_req *req, sd_bus_error *ret_error) +{ + struct json_object *obj = NULL; + int rc; + const sd_bus_error *err; + + err = sd_bus_message_get_error(msg); + if (err != NULL) + afb_req_fail_f(*req, "failed", "DBus-error-name: %s, DBus-error-message: %s", err->name, err->message); + else { + rc = unpacklist(msg, &obj); + if (rc < 0) + afb_req_fail(*req, "failed", "can't unpack"); + else + afb_req_success(*req, obj, NULL); + } + json_object_put(obj); + afb_req_unref(*req); + free(req); + return 1; +} + +/* + * Make a raw call to DBUS method + * The query should have: + * { + * "bus": "optional: 'system' or 'user' (default)" + * "destination": "destination handling the object", + * "path": "object path", + * "interface": "interface of the call", + * "member": "member of the interface of the call", + * "signature": "signature of the arguments", + * "arguments": "ARRAY of arguments" + * } + */ +static void rawcall(struct afb_req req) +{ + struct json_object *obj; + struct json_object *args; + + const char *busname; + const char *destination; + const char *path; + const char *interface; + const char *member; + const char *signature; + + struct sd_bus_message *msg = NULL; + struct sd_bus *bus; + int rc; + + /* get the query */ + obj = afb_req_json(req); + if (obj == NULL) + goto internal_error; + + /* get parameters */ + destination = strval(obj, "destination"); + path = strval(obj, "path"); + interface = strval(obj, "interface"); + member = strval(obj, "member"); + if (path == NULL || member == NULL) + goto bad_request; + + /* get arguments */ + signature = strval(obj, "signature") ? : ""; + args = NULL; + json_object_object_get_ex(obj, "arguments", &args); + + /* get bus */ + busname = strval(obj, "bus"); + if (busname != NULL && !strcmp(busname, "system")) + bus = afb_daemon_get_system_bus(afbitf->daemon); + else + bus = afb_daemon_get_user_bus(afbitf->daemon); + + /* creates the message */ + rc = sd_bus_message_new_method_call(bus, &msg, destination, path, interface, member); + if (rc != 0) + goto internal_error; + rc = packlist(msg, signature, args); + if (rc < 0) + goto bad_request; + + /* */ + rc = sd_bus_call_async(bus, NULL, msg, (void*)on_rawcall_reply, afb_req_store(req), -1); + if (rc < 0) + goto internal_error; + goto cleanup; + +internal_error: + afb_req_fail(req, "failed", "internal error"); + goto cleanup; + +bad_request: + afb_req_fail(req, "failed", "bad request"); + +cleanup: + sd_bus_message_unref(msg); +} + +/* + * array of the verbs exported to afb-daemon + */ +static const struct afb_verb_desc_v1 binding_verbs[] = { + /* VERB'S NAME SESSION MANAGEMENT FUNCTION TO CALL SHORT DESCRIPTION */ + { .name= "rawcall", .session= AFB_SESSION_NONE, .callback= rawcall, .info= "raw call to dbus method" }, + { .name= NULL } /* marker for end of the array */ +}; + +/* + * description of the binding for afb-daemon + */ +static const struct afb_binding binding_description = +{ + /* description conforms to VERSION 1 */ + .type= AFB_BINDING_VERSION_1, + .v1= { /* fills the v1 field of the union when AFB_BINDING_VERSION_1 */ + .prefix= "dbus", /* the API name (or binding name or prefix) */ + .info= "raw dbus binding", /* short description of of the binding */ + .verbs = binding_verbs /* the array describing the verbs of the API */ + } +}; + +/* + * activation function for registering the binding called by afb-daemon + */ +const struct afb_binding *afbBindingV1Register(const struct afb_binding_interface *itf) +{ + afbitf = itf; /* records the interface for accessing afb-daemon */ + return &binding_description; /* returns the description of the binding */ +} + diff --git a/bindings/intrinsics/export.map b/bindings/intrinsics/export.map new file mode 100644 index 00000000..0ef1ac79 --- /dev/null +++ b/bindings/intrinsics/export.map @@ -0,0 +1 @@ +{ global: afbBindingV1Register; local: *; }; diff --git a/bindings/media/CMakeLists.txt b/bindings/media/CMakeLists.txt new file mode 100644 index 00000000..2866dfbc --- /dev/null +++ b/bindings/media/CMakeLists.txt @@ -0,0 +1,18 @@ +INCLUDE(FindPkgConfig) +PKG_CHECK_MODULES(gupnp gupnp-1.0 gupnp-av-1.0 gssdp-1.0 gobject-2.0 gio-2.0) + +IF(gupnp_FOUND) + + MESSAGE(STATUS "gupnp found ; will compile Media binding... (binding)") + + INCLUDE_DIRECTORIES( ${include_dirs} ${gupnp_INCLUDE_DIRS}) + ADD_LIBRARY(media-api MODULE media-api.c media-rygel.c) + SET_TARGET_PROPERTIES(media-api PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" + ) + TARGET_LINK_LIBRARIES(media-api ${link_libraries} ${gupnp_LIBRARIES}) + INSTALL(TARGETS media-api + LIBRARY DESTINATION ${binding_install_dir}) + +ENDIF(gupnp_FOUND) diff --git a/bindings/media/export.map b/bindings/media/export.map new file mode 100644 index 00000000..0ef1ac79 --- /dev/null +++ b/bindings/media/export.map @@ -0,0 +1 @@ +{ global: afbBindingV1Register; local: *; }; diff --git a/bindings/media/media-api.c b/bindings/media/media-api.c new file mode 100644 index 00000000..578d0066 --- /dev/null +++ b/bindings/media/media-api.c @@ -0,0 +1,331 @@ +/* + * Copyright (C) 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE + +#include <string.h> + +#include <json-c/json.h> + +#include "media-api.h" +#include "media-rygel.h" + +#include <afb/afb-plugin.h> +#include <afb/afb-req-itf.h> + +json_object* _rygel_list (mediaCtxHandleT *); + +/* ------ LOCAL HELPER FUNCTIONS --------- */ + +/* private client context creation ; default values */ +static mediaCtxHandleT* initMediaCtx () { + + mediaCtxHandleT *ctx; + + ctx = malloc (sizeof(mediaCtxHandleT)); + ctx->media_server = NULL; + ctx->index = 0; + + return ctx; +} + +/* ------ PUBLIC PLUGIN FUNCTIONS --------- */ + +static void init (struct afb_req request) { /* AFB_SESSION_CHECK */ + + mediaCtxHandleT *ctx = afb_req_context_get(request); + json_object *jresp; + + /* create a private client context */ + if (!ctx) { + ctx = initMediaCtx(); + afb_req_context_set (request, ctx, free); + } + + /* initialize server connection */ + if (!ctx->media_server) + _rygel_init (ctx); + + jresp = json_object_new_object (); + json_object_object_add (jresp, "init", json_object_new_string ("success")); + afb_req_success (request, jresp, "Media - Initialized"); +} + +static void list (struct afb_req request) { /* AFB_SESSION_CHECK */ + + mediaCtxHandleT *ctx = afb_req_context_get(request); + json_object *jresp; + + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + + jresp = _rygel_list (ctx); + + if (!jresp) { + afb_req_fail (request, "failed", "no content found in media server"); + return; + } + + afb_req_success (request, jresp, "Media - Listed"); +} + +static void selecting (struct afb_req request) { /* AFB_SESSION_CHECK */ + + mediaCtxHandleT *ctx = afb_req_context_get(request); + const char *value = afb_req_value (request, "value"); + json_object *jresp; + unsigned int index; + char index_str[5]; + + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + + /* no "?value=" parameter : return current index */ + if (!value) { + snprintf (index_str, sizeof(index_str), "%d", ctx->index); + jresp = json_object_new_object(); + json_object_object_add (jresp, "index", json_object_new_string (index_str)); + } + + /* "?value=" parameter is negative */ + else if (atoi(value) < 0) { + afb_req_fail (request, "failed", "chosen index cannot be negative"); + return; + } + + /* "?value=" parameter is positive */ + else if (atoi(value) >= 0) { + index = (unsigned int) atoi(value); + + if (!_rygel_select (ctx, index)) { + afb_req_fail (request, "failed", "chosen index superior to current media count"); + return; + } + + ctx->index = index; + jresp = json_object_new_object(); + json_object_object_add (jresp, "index", json_object_new_string (value)); + } + else + jresp = NULL; + + afb_req_success (request, jresp, "Media - Listed"); +} + +static void play (struct afb_req request) { /* AFB_SESSION_CHECK */ + + mediaCtxHandleT *ctx = afb_req_context_get(request); + json_object *jresp; + + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + + if (!_rygel_do (ctx, PLAY, NULL)) { + afb_req_fail (request, "failed", "could not play chosen media"); + return; + } + + jresp = json_object_new_object (); + json_object_object_add (jresp, "play", json_object_new_string ("success")); + afb_req_success (request, jresp, "Media - Listed"); +} + +static void stop (struct afb_req request) { /* AFB_SESSION_CHECK */ + + mediaCtxHandleT *ctx = afb_req_context_get(request); + json_object *jresp; + + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + + if (!_rygel_do (ctx, STOP, NULL)) { + afb_req_fail (request, "failed", "could not stop chosen media"); + return; + } + + jresp = json_object_new_object (); + json_object_object_add (jresp, "stop", json_object_new_string ("success")); + afb_req_success (request, jresp, "Media - Stopped"); +} + +static void pausing (struct afb_req request) { /* AFB_SESSION_CHECK */ + + mediaCtxHandleT *ctx = afb_req_context_get(request); + json_object *jresp; + + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + + if (!_rygel_do (ctx, PAUSE, NULL)) { + afb_req_fail (request, "failed", "could not pause chosen media"); + return; + } + + jresp = json_object_new_object(); + json_object_object_add (jresp, "pause", json_object_new_string ("success")); + afb_req_success (request, jresp, "Media - Paused"); +} + +static void seek (struct afb_req request) { /* AFB_SESSION_CHECK */ + + mediaCtxHandleT *ctx = afb_req_context_get(request); + const char *value = afb_req_value (request, "value"); + json_object *jresp; + + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + + /* no "?value=" parameter : return error */ + if (!value) { + afb_req_fail (request, "failed", "you must provide a time"); + return; + } + + if (!_rygel_do (ctx, SEEK, (char *)value)) { + afb_req_fail (request, "failed", "could not seek chosen media"); + return; + } + + jresp = json_object_new_object(); + json_object_object_add (jresp, "seek", json_object_new_string ("success")); + afb_req_success (request, jresp, "Media - Sought"); +} + +static char *renamed_filename(struct afb_arg argfile) +{ + char *result; + const char *e = strrchr(argfile.path, '/'); + if (e == NULL) + result = strdup(argfile.value); + else { + result = malloc((++e - argfile.path) + strlen(argfile.value) + 1); + if (result != NULL) + strcpy(stpncpy(result, argfile.path, e - argfile.path), argfile.value); + } + return result; +} + +static void on_uploaded(struct afb_req *prequest, int status) +{ + struct afb_req request = afb_req_unstore(prequest); + struct afb_arg argfile = afb_req_get(request, "file-upload"); + char *file = renamed_filename(argfile); + if (file != NULL) + unlink(file); + free(file); + if (status) + afb_req_fail (request, "failed", "expected file not received"); + else + afb_req_success_f (request, NULL, "uploaded file %s", argfile.value); + afb_req_unref(request); +} + +static void upload (struct afb_req request) { /* AFB_SESSION_CHECK */ + + mediaCtxHandleT *ctx = afb_req_context_get(request); + struct afb_req *prequest; + struct afb_arg argfile; + char *path; + + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + + /* get the file */ + argfile = afb_req_get(request, "file-upload"); + if (!argfile.value || !argfile.path) { + afb_req_fail (request, "failed", "expected file not received"); + return; + } + + /* rename the file */ + path = renamed_filename(argfile); + if (path == NULL) { + afb_req_fail (request, "failed", "out of memory"); + return; + } + if (rename(argfile.path, path) != 0) { + free(path); + afb_req_fail (request, "failed", "system error"); + return; + } + + /* for asynchronous processing */ + prequest = afb_req_store(request); + if (path == NULL) { + unlink(path); + afb_req_fail (request, "failed", "out of memory"); + } + else if (!_rygel_upload (ctx, path, (void*)on_uploaded, prequest)) { + afb_req_unref(afb_req_unstore(prequest)); + unlink(path); + afb_req_fail (request, "failed", "Error when uploading file to media server... could not complete"); + } + free(path); +} + +static void ping (struct afb_req request) { /* AFB_SESSION_NONE */ + afb_req_success (request, NULL, "Media - Ping succeeded"); +} + + +static const struct AFB_verb_desc_v1 verbs[]= { + {"init" , AFB_SESSION_CHECK, init , "Media API - init" }, + {"list" , AFB_SESSION_CHECK, list , "Media API - list" }, + {"select" , AFB_SESSION_CHECK, selecting , "Media API - select" }, + {"play" , AFB_SESSION_CHECK, play , "Media API - play" }, + {"stop" , AFB_SESSION_CHECK, stop , "Media API - stop" }, + {"pause" , AFB_SESSION_CHECK, pausing , "Media API - pause" }, + {"seek" , AFB_SESSION_CHECK, seek , "Media API - seek" }, +// {"upload" , AFB_SESSION_CHECK, upload , "Media API - upload" }, + {"ping" , AFB_SESSION_NONE, ping , "Media API - ping" }, + {NULL} +}; + +static const struct AFB_plugin pluginDesc = { + .type = AFB_PLUGIN_VERSION_1, + .v1 = { + .info = "Application Framework Binder - Media plugin", + .prefix = "media", + .verbs = verbs + } +}; + +const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *itf) +{ + return &pluginDesc; +} diff --git a/bindings/media/media-api.h b/bindings/media/media-api.h new file mode 100644 index 00000000..c1d764c2 --- /dev/null +++ b/bindings/media/media-api.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MEDIA_API_H +#define MEDIA_API_H + +/* -------------- PLUGIN DEFINITIONS ----------------- */ + +/* private client context [will be destroyed when client leaves] */ +typedef struct { + void *media_server; /* handle to implementation (Rygel...) */ + unsigned int index; /* currently selected media file */ +} mediaCtxHandleT; + +#endif /* MEDIA_API_H */ diff --git a/bindings/media/media-rygel.c b/bindings/media/media-rygel.c new file mode 100644 index 00000000..00327795 --- /dev/null +++ b/bindings/media/media-rygel.c @@ -0,0 +1,721 @@ +/* + * Copyright (C) 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#include <stdio.h> +#include <string.h> + +#include "media-api.h" +#include "media-rygel.h" + +static void _rygel_device_cb (GUPnPControlPoint *, GUPnPDeviceProxy *, gpointer); +static void _rygel_av_transport_cb (GUPnPControlPoint *, GUPnPDeviceProxy *, gpointer); +static void _rygel_content_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); +static void _rygel_metadata_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); +static void _rygel_select_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); +static void _rygel_upload_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); +static void _rygel_transfer_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); +static void _rygel_do_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); + +static unsigned int client_count = 0; +static struct dev_ctx **dev_ctx = NULL; + +/* -------------- MEDIA RYGEL IMPLEMENTATION ---------------- */ + +/* --- PUBLIC FUNCTIONS --- */ + +unsigned char _rygel_init (mediaCtxHandleT *ctx) { + + GMainContext *loop; + GUPnPContext *context; + GUPnPControlPoint *control_point; + gint handler_cb; + struct timeval tv_start, tv_now; + + context = gupnp_context_new (NULL, NULL, 0, NULL); + + control_point = gupnp_control_point_new (context, URN_MEDIA_SERVER); + + handler_cb = g_signal_connect (control_point, "device-proxy-available", + G_CALLBACK (_rygel_device_cb), ctx); + + /* start searching for servers */ + gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (control_point), TRUE); + + loop = g_main_context_default (); + + /* 5 seconds should be sufficient to find Rygel */ + gettimeofday (&tv_start, NULL); + gettimeofday (&tv_now, NULL); + while (tv_now.tv_sec - tv_start.tv_sec <= 5) { + + g_main_context_iteration (loop, FALSE); + + if (ctx->media_server) + break; + gettimeofday (&tv_now, NULL); + } + /* fail if we found no server */ + if (!ctx->media_server) + return 0; + + /* we have found the server ; stop looking for it... */ + g_signal_handler_disconnect (control_point, handler_cb); + + dev_ctx[client_count]->loop = loop; + dev_ctx[client_count]->context = context; + dev_ctx[client_count]->av_transport = NULL; + dev_ctx[client_count]->state = STOP; + dev_ctx[client_count]->target_state = STOP; + dev_ctx[client_count]->action_args = NULL; + dev_ctx[client_count]->transfer_started = 0; + + client_count++; + + return 1; +} + +void _rygel_free (mediaCtxHandleT *ctx) { + + dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server; + + client_count--; + + g_main_context_unref (dev_ctx_c->loop); + dev_ctx_c->loop = NULL; + dev_ctx_c->context = NULL; + dev_ctx_c->device_info = NULL; + dev_ctx_c->av_transport = NULL; + dev_ctx_c->content_dir = NULL; + dev_ctx_c->content_res = NULL; +} + +json_object* _rygel_list (mediaCtxHandleT *ctx) { + + dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server; + json_object *json_o, *json_a; + char *raw, *start, *end, *id, *title; + int length, i = 0; + + if (!dev_ctx_c) + return NULL; + + raw = _rygel_list_raw (dev_ctx_c, NULL); + if (!raw) + return NULL; + + start = strstr (raw, "<dc:title>"); + if (!start) + return NULL; + + json_o = json_object_new_object (); + json_a = json_object_new_array (); + while (start) { + json_object *json_i, *json_id, *json_title; + + start = strstr (start, "<dc:title>"); + if (!start) break; + end = strstr (start, "</dc:title>"); + start += 10; + length = end - start; + + asprintf (&id, "%02d", i); + + title = (char*) malloc (length+1); + strncpy (title, start, length); + title[length] = '\0'; + + json_i = json_object_new_object (); + json_id = json_object_new_string (id); + json_title = json_object_new_string (title); + json_object_object_add (json_i, "id", json_id); + json_object_object_add (json_i, "title", json_title); + json_object_array_add (json_a, json_i); + + free (id); free (title); + i++; + } + + json_object_object_add (json_o, "list", json_a); + + return json_o; +} + +unsigned char _rygel_select (mediaCtxHandleT *ctx, unsigned int index) { + + dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server; + unsigned int count; + + if (!dev_ctx_c) + return 0; + + if (!_rygel_list_raw (dev_ctx_c, &count) || + index >= count) + return 0; + + if (ctx->index != index) + dev_ctx_c->state = STOP; + + return 1; +} + +unsigned char _rygel_upload (mediaCtxHandleT *ctx, const char *path, void (*oncompletion)(void*,int), void *closure) { + + dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server; + char *raw, *upload_id; + + if (!dev_ctx_c) + return 0; + + raw = _rygel_list_raw (dev_ctx_c, NULL); + if (!raw) + return 0; + + /* for now, we always use the same upload container id */ + upload_id = _rygel_find_upload_id (dev_ctx_c, raw); + + return _rygel_start_uploading (dev_ctx_c, strdup(path), upload_id); +} + +unsigned char _rygel_do (mediaCtxHandleT *ctx, State state, char *args) { + + dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server; + unsigned int index = ctx->index; + unsigned int count; + char *raw, *id, *metadata, *uri; + + if (!dev_ctx_c || dev_ctx_c->state == state) + return 0; + + raw = _rygel_list_raw (dev_ctx_c, &count); + if (!raw || index >= count) + return 0; + + id = _rygel_find_id_for_index (dev_ctx_c, raw, index); + metadata = _rygel_find_metadata_for_id (dev_ctx_c, id); + uri = _rygel_find_uri_for_metadata (dev_ctx_c, metadata); + + return _rygel_start_doing (dev_ctx_c, uri, metadata, state, args); +} + +/* --- LOCAL HELPER FUNCTIONS --- */ + +char* _rygel_list_raw (dev_ctx_T* dev_ctx_c, unsigned int *count) { + + GUPnPServiceProxy *content_dir_proxy; + struct timeval tv_start, tv_now; + + dev_ctx_c->content_res = NULL; + dev_ctx_c->content_num = 0; + content_dir_proxy = GUPNP_SERVICE_PROXY (dev_ctx_c->content_dir); + + gupnp_service_proxy_begin_action (content_dir_proxy, "Browse", _rygel_content_cb, dev_ctx_c, + "ObjectID", G_TYPE_STRING, "Filesystem", + "BrowseFlag", G_TYPE_STRING, "BrowseDirectChildren", + "Filter", G_TYPE_STRING, "@childCount", + "StartingIndex", G_TYPE_UINT, 0, + "RequestedCount", G_TYPE_UINT, 64, + "SortCriteria", G_TYPE_STRING, "", + NULL); + + gettimeofday (&tv_start, NULL); + gettimeofday (&tv_now, NULL); + while (tv_now.tv_sec - tv_start.tv_sec <= 5) { + + g_main_context_iteration (dev_ctx_c->loop, FALSE); + + if (dev_ctx_c->content_res) + break; + gettimeofday (&tv_now, NULL); + } + + if (count) *count = dev_ctx_c->content_num; + return dev_ctx_c->content_res; +} + +char* _rygel_find_upload_id (dev_ctx_T* dev_ctx_c, char *raw) { + + char *found; + char id[33]; + + found = strstr (raw, "parentID=\""); + found += 10; + + /* IDs are 32-bit strings */ + strncpy (id, found, 32); + id[32] = '\0'; + + return strdup (id); +} + +char* _rygel_find_id_for_index (dev_ctx_T* dev_ctx_c, char *raw, unsigned int index) { + + char *found = raw; + char id[33]; + int i; + + for (i = 0; i <= index; i++) { + found = strstr (found, "item id="); + found += 9; + + if (i == index) { + /* IDs are 32-bit strings */ + strncpy (id, found, 32); + id[32] = '\0'; + } + } + + return strdup (id); +} + +char* _rygel_find_metadata_for_id (dev_ctx_T* dev_ctx_c, char *id) { + + GUPnPServiceProxy *content_dir_proxy; + struct timeval tv_start, tv_now; + + dev_ctx_c->content_res = NULL; + + content_dir_proxy = GUPNP_SERVICE_PROXY (dev_ctx_c->content_dir); + + gupnp_service_proxy_begin_action (content_dir_proxy, "Browse", _rygel_metadata_cb, dev_ctx_c, + "ObjectID", G_TYPE_STRING, id, + "BrowseFlag", G_TYPE_STRING, "BrowseMetadata", + "Filter", G_TYPE_STRING, "*", + "StartingIndex", G_TYPE_UINT, 0, + "RequestedCount", G_TYPE_UINT, 0, + "SortCriteria", G_TYPE_STRING, "", + NULL); + + gettimeofday (&tv_start, NULL); + gettimeofday (&tv_now, NULL); + while (tv_now.tv_sec - tv_start.tv_sec <= 5) { + + g_main_context_iteration (dev_ctx_c->loop, FALSE); + + if (dev_ctx_c->content_res) + break; + gettimeofday (&tv_now, NULL); + } + + return dev_ctx_c->content_res; +} + +char* _rygel_find_uri_for_metadata (dev_ctx_T* dev_ctx_c, char *metadata) { + + char *start, *end, *uri = NULL; + int length; + + /* position ourselves after the first "<res " tag */ + start = strstr (metadata, "<res "); + + while (start) { + start = strstr (start, "http://"); + if (!start) break; + end = strstr (start, "</res>"); + length = end - start; + + + uri = (char *)malloc (length + 1); + strncpy (uri, start, length); + uri[length] = '\0'; + /* if the URI contains "primary_http", it is the main one ; stop here...*/ + if (strstr (uri, "primary_http")) + break; + + free (uri); start = end; + } + + return uri; +} + +char * _rygel_time_for_string (char *string) { + + int total_seconds; + unsigned int hours, minutes, seconds; + char *time; + + total_seconds = atoi (string); + hours = total_seconds / 3600; + minutes = (total_seconds / 60) - (hours * 60); + seconds = total_seconds - (hours * 3600) - (minutes * 60); + + asprintf (&time, "%u:%02u:%02u", hours, minutes, seconds); + + return time; +} + +unsigned char _rygel_start_uploading (dev_ctx_T* dev_ctx_c, char *path, char *upload_id) { + + GUPnPServiceProxy *content_dir_proxy; + GUPnPDIDLLiteWriter *didl_writer; + GUPnPDIDLLiteObject *didl_object; + char *didl, *content_type, *mime_type, *upnp_class; + struct timeval tv_start, tv_now; + + didl_writer = gupnp_didl_lite_writer_new (NULL); + didl_object = GUPNP_DIDL_LITE_OBJECT (gupnp_didl_lite_writer_add_item (didl_writer)); + + /* create the metadata for the file */ + gupnp_didl_lite_object_set_parent_id (didl_object, upload_id); + gupnp_didl_lite_object_set_id (didl_object, ""); + gupnp_didl_lite_object_set_restricted (didl_object, FALSE); + gupnp_didl_lite_object_set_title (didl_object, g_path_get_basename (path)); + /* deduce the UPnP class from the MIME type ("audio/ogg" e.g.) */ + content_type = g_content_type_guess (path, NULL, 0, NULL); + mime_type = g_content_type_get_mime_type (content_type); + if (strstr (mime_type, "audio/")) + upnp_class = strdup ("object.item.audioItem.musicTrack"); + else if (strstr (mime_type, "video/")) + upnp_class = strdup ("object.item.videoItem"); + else if (strstr (mime_type, "image/")) + upnp_class = strdup ("object.item.imageItem"); + else + upnp_class = strdup ("object.item"); + gupnp_didl_lite_object_set_upnp_class (didl_object, upnp_class); + didl = gupnp_didl_lite_writer_get_string (didl_writer); + + dev_ctx_c->transfer_path = path; + dev_ctx_c->transfer_started = 0; + content_dir_proxy = GUPNP_SERVICE_PROXY (dev_ctx_c->content_dir); + + gupnp_service_proxy_begin_action (content_dir_proxy, "CreateObject", _rygel_upload_cb, dev_ctx_c, + "ContainerID", G_TYPE_STRING, upload_id, + "Elements", G_TYPE_STRING, didl, + NULL); + + gettimeofday (&tv_start, NULL); + gettimeofday (&tv_now, NULL); + while (tv_now.tv_sec - tv_start.tv_sec <= 5) { + + g_main_context_iteration (dev_ctx_c->loop, FALSE); + + if (dev_ctx_c->transfer_started) + break; + gettimeofday (&tv_now, NULL); + } + if (!dev_ctx_c->transfer_started) + return 0; + + return 1; +} + +unsigned char _rygel_start_doing (dev_ctx_T* dev_ctx_c, char *uri, char *metadata, State state, char *args) { + + GUPnPServiceProxy *av_transport_proxy; + struct timeval tv_start, tv_now; + + if (!dev_ctx_c->av_transport) { + if (!_rygel_find_av_transport (dev_ctx_c)) + return 0; + } + dev_ctx_c->target_state = state; + dev_ctx_c->action_args = args; + av_transport_proxy = GUPNP_SERVICE_PROXY (dev_ctx_c->av_transport); + + gupnp_service_proxy_begin_action (av_transport_proxy, "SetAVTransportURI", _rygel_select_cb, dev_ctx_c, + "InstanceID", G_TYPE_UINT, 0, + "CurrentURI", G_TYPE_STRING, uri, + "CurrentURIMetaData", G_TYPE_STRING, metadata, + NULL); + + gettimeofday (&tv_start, NULL); + gettimeofday (&tv_now, NULL); + while (tv_now.tv_sec - tv_start.tv_sec <= 5) { + + g_main_context_iteration (dev_ctx_c->loop, FALSE); + + if (dev_ctx_c->state == state) + break; + gettimeofday (&tv_now, NULL); + } + if (dev_ctx_c->state != state) + return 0; + + return 1; +} + +unsigned char _rygel_find_av_transport (dev_ctx_T* dev_ctx_c) { + + GUPnPControlPoint *control_point; + gint handler_cb; + struct timeval tv_start, tv_now; + + control_point = gupnp_control_point_new (dev_ctx_c->context, URN_MEDIA_RENDERER); + + handler_cb = g_signal_connect (control_point, "device-proxy-available", + G_CALLBACK (_rygel_av_transport_cb), dev_ctx_c); + + gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (control_point), TRUE); + + gettimeofday (&tv_start, NULL); + gettimeofday (&tv_now, NULL); + while (tv_now.tv_sec - tv_start.tv_sec <= 5) { + + g_main_context_iteration (dev_ctx_c->loop, FALSE); + + if (dev_ctx_c->av_transport) + break; + gettimeofday (&tv_now, NULL); + } + g_signal_handler_disconnect (control_point, handler_cb); + + if (!dev_ctx_c->av_transport) + return 0; + + return 1; +} + + + /* ---- LOCAL CALLBACK FUNCTIONS ---- */ + +static void _rygel_device_cb (GUPnPControlPoint *point, GUPnPDeviceProxy *proxy, + gpointer data) { + + mediaCtxHandleT *ctx = (mediaCtxHandleT*)data; + GUPnPDeviceInfo *device_info; + GUPnPServiceInfo *content_dir; + const char *device_name; + + device_info = GUPNP_DEVICE_INFO (proxy); + device_name = gupnp_device_info_get_model_name (device_info); + content_dir = gupnp_device_info_get_service (device_info, URN_CONTENT_DIR); + + if (strcmp (device_name, "Rygel") != 0) + return; + if (!content_dir) + return; + + /* allocate the global array if it has not been not done */ + if (!dev_ctx) + dev_ctx = (dev_ctx_T**) malloc (sizeof(dev_ctx_T)); + else + dev_ctx = (dev_ctx_T**) realloc (dev_ctx, (client_count+1)*sizeof(dev_ctx_T)); + + /* create an element for the client in the global array */ + dev_ctx[client_count] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T)); + dev_ctx[client_count]->device_info = device_info; + dev_ctx[client_count]->content_dir = content_dir; + + /* make the client context aware of it */ + ctx->media_server = (void*)dev_ctx[client_count]; +} + +static void _rygel_av_transport_cb (GUPnPControlPoint *point, GUPnPDeviceProxy *proxy, + gpointer data) { + + dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data; + GUPnPDeviceInfo *device_info; + GUPnPServiceInfo *av_transport; + + device_info = GUPNP_DEVICE_INFO (proxy); + av_transport = gupnp_device_info_get_service (device_info, URN_AV_TRANSPORT); + + dev_ctx_c->av_transport = av_transport; +} + +static void _rygel_content_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxyAction *action, + gpointer data) { + + dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data; + GUPnPServiceProxy *content_dir_proxy = GUPNP_SERVICE_PROXY (content_dir); + GError *error; + char *result; + guint32 number_returned; + guint32 total_matches; + char *found; + char subid[33]; + + gupnp_service_proxy_end_action (content_dir, action, &error, + "Result", G_TYPE_STRING, &result, + "NumberReturned", G_TYPE_UINT, &number_returned, + "TotalMatches", G_TYPE_UINT, &total_matches, + NULL); + + if (number_returned == 0) + return; + + if (number_returned == 1) { + found = strstr (result, "id=\""); + found += 4; + strncpy (subid, found, 32); subid[32] = '\0'; + + gupnp_service_proxy_begin_action (content_dir_proxy, "Browse", _rygel_content_cb, dev_ctx_c, + "ObjectID", G_TYPE_STRING, subid, + "BrowseFlag", G_TYPE_STRING, "BrowseDirectChildren", + "Filter", G_TYPE_STRING, "@childCount", + "StartingIndex", G_TYPE_UINT, 0, + "RequestedCount", G_TYPE_UINT, 64, + "SortCriteria", G_TYPE_STRING, "", + NULL); + return; + } + + if (number_returned > 1) { + dev_ctx_c->content_res = result; + dev_ctx_c->content_num = number_returned; + } +} + +static void _rygel_metadata_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxyAction *action, + gpointer data) { + + dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data; + GError *error; + char *result; + + gupnp_service_proxy_end_action (content_dir, action, &error, + "Result", G_TYPE_STRING, &result, + NULL); + + dev_ctx_c->content_res = result; +} + +static void _rygel_select_cb (GUPnPServiceProxy *av_transport, GUPnPServiceProxyAction *action, + gpointer data) +{ + + dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data; + GUPnPServiceProxy *av_transport_proxy; + GError *error; + char *time; + struct timeval tv_start, tv_now; + + av_transport_proxy = GUPNP_SERVICE_PROXY (av_transport); + + gupnp_service_proxy_end_action (av_transport, action, &error, NULL); + + switch (dev_ctx_c->target_state) { + case PLAY: + gupnp_service_proxy_begin_action (av_transport_proxy, "Play", _rygel_do_cb, dev_ctx_c, + "InstanceID", G_TYPE_UINT, 0, + "Speed", G_TYPE_STRING, "1", + NULL); + break; + case PAUSE: + gupnp_service_proxy_begin_action (av_transport_proxy, "Pause", _rygel_do_cb, dev_ctx_c, + "InstanceID", G_TYPE_UINT, 0, + NULL); + break; + case STOP: + gupnp_service_proxy_begin_action (av_transport_proxy, "Stop", _rygel_do_cb, dev_ctx_c, + "InstanceID", G_TYPE_UINT, 0, + NULL); + break; + case SEEK: + time = _rygel_time_for_string (dev_ctx_c->action_args); + gupnp_service_proxy_begin_action (av_transport_proxy, "Seek", _rygel_do_cb, dev_ctx_c, + "InstanceID", G_TYPE_UINT, 0, + "Unit", G_TYPE_STRING, "ABS_TIME", + "Target", G_TYPE_STRING, time, + NULL); + default: + break; + } + + gettimeofday (&tv_start, NULL); + gettimeofday (&tv_now, NULL); + while (tv_now.tv_sec - tv_start.tv_sec <= 5) { + + g_main_context_iteration (dev_ctx_c->loop, FALSE); + + if (dev_ctx_c->state == dev_ctx_c->target_state) + break; + gettimeofday (&tv_now, NULL); + } +} + +static void _rygel_upload_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxyAction *action, + gpointer data) +{ + dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data; + GUPnPServiceProxy *content_dir_proxy; + GError *error; + char *result, *start, *end, *dst_uri, *src_uri; + int length; + struct timeval tv_start, tv_now; + + content_dir_proxy = GUPNP_SERVICE_PROXY (content_dir); + + if (!gupnp_service_proxy_end_action (content_dir, action, &error, + "Result", G_TYPE_STRING, &result, + NULL)) + return; + + start = strstr (result, "<res importUri=\""); + if (!start) + return; + + start += 16; + end = strstr (start, "\""); + length = end - start; + + dst_uri = (char*) malloc(length+1); + strncpy (dst_uri, start, length); + dst_uri[length] = '\0'; + + asprintf (&src_uri, "http://%s:%u%s", gupnp_context_get_host_ip (dev_ctx_c->context), + gupnp_context_get_port (dev_ctx_c->context), + dev_ctx_c->transfer_path); + + /* host the file */ + gupnp_context_host_path (dev_ctx_c->context, dev_ctx_c->transfer_path, + dev_ctx_c->transfer_path); + + gupnp_service_proxy_begin_action (content_dir_proxy, "ImportResource", _rygel_transfer_cb, dev_ctx_c, + "SourceURI", G_TYPE_STRING, src_uri, + "DestinationURI", G_TYPE_STRING, dst_uri, + NULL); + + gettimeofday (&tv_start, NULL); + gettimeofday (&tv_now, NULL); + while (tv_now.tv_sec - tv_start.tv_sec <= 5) { + + g_main_context_iteration (dev_ctx_c->loop, FALSE); + + if (dev_ctx_c->transfer_started) + break; + gettimeofday (&tv_now, NULL); + } +} + +static void _rygel_transfer_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxyAction *action, + gpointer data) +{ + dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data; + GError *error; + guint transfer_id; + + if (!gupnp_service_proxy_end_action (content_dir, action, &error, + "TransferID", G_TYPE_UINT, &transfer_id, + NULL)) + return; + + dev_ctx_c->transfer_started = 1; +} + +static void _rygel_do_cb (GUPnPServiceProxy *av_transport, GUPnPServiceProxyAction *action, + gpointer data) +{ + dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data; + GError *error; + + if (!gupnp_service_proxy_end_action (av_transport, action, &error, + NULL)) + return; + + dev_ctx_c->state = dev_ctx_c->target_state; +} diff --git a/bindings/media/media-rygel.h b/bindings/media/media-rygel.h new file mode 100644 index 00000000..951c6b89 --- /dev/null +++ b/bindings/media/media-rygel.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MEDIA_RYGEL_H +#define MEDIA_RYGEL_H + +/* --------------- MEDIA RYGEL DEFINITIONS ------------------ */ + +#include <sys/time.h> +#include <json-c/json.h> +#include <libgupnp/gupnp-control-point.h> +#include <libgupnp-av/gupnp-av.h> + +#include "media-api.h" + +#define URN_MEDIA_SERVER "urn:schemas-upnp-org:device:MediaServer:1" +#define URN_MEDIA_RENDERER "urn:schemas-upnp-org:device:MediaRenderer:1" +#define URN_CONTENT_DIR "urn:schemas-upnp-org:service:ContentDirectory" +#define URN_AV_TRANSPORT "urn:schemas-upnp-org:service:AVTransport" + +typedef enum { PLAY, PAUSE, STOP, SEEK } State; +typedef struct dev_ctx dev_ctx_T; + +struct dev_ctx { + GMainContext *loop; + GUPnPContext *context; + GUPnPDeviceInfo *device_info; + GUPnPServiceInfo *content_dir; + GUPnPServiceInfo *av_transport; + char *content_res; + int content_num; + State state; + State target_state; + char *action_args; + char *transfer_path; + unsigned char transfer_started; +}; + +unsigned char _rygel_init (mediaCtxHandleT *); +void _rygel_free (mediaCtxHandleT *); +json_object* _rygel_list (mediaCtxHandleT *); +unsigned char _rygel_select (mediaCtxHandleT *, unsigned int); +unsigned char _rygel_upload (mediaCtxHandleT *ctx, const char *path, void (*oncompletion)(void*,int), void *closure); +unsigned char _rygel_do (mediaCtxHandleT *, State, char *); + +char* _rygel_list_raw (dev_ctx_T *, unsigned int *); +char* _rygel_find_upload_id (dev_ctx_T *, char *); +char* _rygel_find_id_for_index (dev_ctx_T *, char *, unsigned int); +char* _rygel_find_metadata_for_id (dev_ctx_T *, char *); +char* _rygel_find_uri_for_metadata (dev_ctx_T *, char *); +unsigned char _rygel_start_uploading (dev_ctx_T *, char *, char *); +unsigned char _rygel_start_doing (dev_ctx_T *, char *, char *, State, char *); +unsigned char _rygel_find_av_transport (dev_ctx_T *); +#endif /* MEDIA_RYGEL_H */ diff --git a/bindings/radio/CMakeLists.txt b/bindings/radio/CMakeLists.txt new file mode 100644 index 00000000..4df7adb2 --- /dev/null +++ b/bindings/radio/CMakeLists.txt @@ -0,0 +1,17 @@ +INCLUDE(FindPkgConfig) +PKG_CHECK_MODULES(librtlsdr librtlsdr>=0.5.0) + +IF(librtlsdr_FOUND) + + MESSAGE(STATUS "librtlsdr found ; will compile Radio binding... (binding)") + INCLUDE_DIRECTORIES(${include_dirs} ${librtlsdr_INCLUDE_DIRS}) + ADD_LIBRARY(radio-api MODULE radio-api.c radio-rtlsdr.c) + SET_TARGET_PROPERTIES(radio-api PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" + ) + TARGET_LINK_LIBRARIES(radio-api ${link_libraries} ${librtlsdr_LIBRARIES} -lm) + INSTALL(TARGETS radio-api + LIBRARY DESTINATION ${binding_install_dir}) + +ENDIF(librtlsdr_FOUND) diff --git a/bindings/radio/export.map b/bindings/radio/export.map new file mode 100644 index 00000000..0ef1ac79 --- /dev/null +++ b/bindings/radio/export.map @@ -0,0 +1 @@ +{ global: afbBindingV1Register; local: *; }; diff --git a/bindings/radio/radio-api.c b/bindings/radio/radio-api.c new file mode 100644 index 00000000..0ed60e1c --- /dev/null +++ b/bindings/radio/radio-api.c @@ -0,0 +1,376 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#include <strings.h> +#include <json-c/json.h> + +#include "radio-api.h" +#include "radio-rtlsdr.h" + +#include <afb/afb-plugin.h> +#include <afb/afb-req-itf.h> + +/* ******************************************************** + + FULUP integration proposal with client session context + + ******************************************************** */ + +/* ------ LOCAL HELPER FUNCTIONS --------- */ + +static pluginHandleT *the_radio = NULL; + +/* detect new radio devices */ +void updateRadioDevList(pluginHandleT *handle) { + + int idx; + + // loop on existing radio if any + for (idx = 0; idx < _radio_dev_count(); idx++) { + if (idx == MAX_RADIO) break; + handle->radios[idx] = calloc(1, sizeof(radioDevT)); /* use calloc to set "used" to FALSE */ + handle->radios[idx]->name = (char *) _radio_dev_name(idx); + } + handle->devCount = _radio_dev_count(); +} + +/* global plugin context creation ; at loading time [radio devices might not be visible] */ +static void initRadioPlugin() { + + pluginHandleT *handle; + + handle = calloc (1, sizeof(pluginHandleT)); + updateRadioDevList (handle); + the_radio = handle; +} + +/* private client context creation ; default values */ +static radioCtxHandleT* initRadioCtx () { + + radioCtxHandleT *ctx; + + ctx = malloc (sizeof(radioCtxHandleT)); + ctx->radio = NULL; + ctx->idx = -1; + ctx->mode = FM; + ctx->freq = 100.0; + ctx->mute = 0; + ctx->is_playing = 0; + + return ctx; +} + +/* reserve a radio device for requesting client, power it on */ +unsigned char reserveRadio (pluginHandleT *handle, radioCtxHandleT *ctx) { + + unsigned int idx; + + /* loop on all devices, find an unused one */ + for (idx = 0; idx < _radio_dev_count(); idx++) { + if (idx == MAX_RADIO) break; + if (handle->radios[idx]->used == FALSE) goto found_radio; /* found one */ + } + return 0; + + found_radio: + /* try to power it on, passing client context info such as frequency... */ + + _radio_on (idx, ctx); + /* TODO : try to re-iterate from the next ones if it failed ! */ + + /* globally mark it as reserved */ + handle->radios[idx]->used = TRUE; + + /* store relevant info to client context (direct pointer, index) */ + ctx->radio = handle->radios[idx]; + ctx->idx = idx; + + return 1; +} + +/* free a radio device from requesting client, power it off */ +unsigned char releaseRadio (pluginHandleT *handle, radioCtxHandleT *ctx) { + + /* stop playing if it was doing this (blocks otherwise) */ + if (ctx->is_playing) { + ctx->is_playing = 0; + _radio_stop (ctx->idx); + } + + /* power it off */ + _radio_off (ctx->idx); + + /* globally mark it as free */ + handle->radios[ctx->idx]->used = FALSE; + + /* clean client context */ + ctx->radio = NULL; + ctx->idx = -1; + + return 1; +} + +/* called when client session dies [e.g. client quits for more than 15mns] */ +static void freeRadio (void *context) { + + releaseRadio (the_radio, context); + free (context); +} + + +/* ------ PUBLIC PLUGIN FUNCTIONS --------- */ + +static void init (struct afb_req request) { /* AFB_SESSION_CHECK */ + + radioCtxHandleT *ctx = afb_req_context_get (request); + json_object *jresp; + + /* create a global plugin handle */ + if (!the_radio) + initRadioPlugin(); + + /* create a private client context */ + if (!ctx) { + ctx = initRadioCtx(); + afb_req_context_set (request, ctx, free); + } + + jresp = json_object_new_object(); + json_object_object_add(jresp, "init", json_object_new_string ("success")); + afb_req_success (request, jresp, "Radio initialized"); +} + +static void power (struct afb_req request) { /* AFB_SESSION_CHECK */ + + pluginHandleT *handle = the_radio; + radioCtxHandleT *ctx = afb_req_context_get (request); + const char *value = afb_req_value (request, "value"); + json_object *jresp; + + if (!ctx) { + afb_req_fail (request, "failed", "you must call 'init' first"); + return; + } + jresp = json_object_new_object(); + + /* no "?value=" parameter : return current state */ + if (!value) { + ctx->radio ? + json_object_object_add (jresp, "power", json_object_new_string ("on")) + : json_object_object_add (jresp, "power", json_object_new_string ("off")); + afb_req_success (request, jresp, "Radio - Power status obtained"); + return; + } + + /* "?value=" parameter is "1" or "true" */ + else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) { + if (!ctx->radio) { + if (!reserveRadio (handle, ctx)) { + afb_req_fail (request, "failed", "no more radio devices available"); + return; + } + } + json_object_object_add (jresp, "power", json_object_new_string ("on")); + } + + /* "?value=" parameter is "0" or "false" */ + else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) { + if (ctx->radio) { + if (!releaseRadio (handle, ctx)) { + afb_req_fail (request, "failed", "Unable to release radio device"); + return; + } + } + json_object_object_add (jresp, "power", json_object_new_string ("off")); + } + else + jresp = NULL; + + afb_req_success (request, jresp, "Radio - Power set"); +} + +static void mode (struct afb_req request) { /* AFB_SESSION_CHECK */ + + radioCtxHandleT *ctx = afb_req_context_get (request); + const char *value = afb_req_value (request, "value"); + json_object *jresp; + + if (!ctx) { + afb_req_fail (request, "failed", "you must call 'init' first"); + return; + } + jresp = json_object_new_object(); + + /* no "?value=" parameter : return current state */ + if (!value || !ctx->radio) { + ctx->mode ? + json_object_object_add (jresp, "mode", json_object_new_string ("AM")) + : json_object_object_add (jresp, "mode", json_object_new_string ("FM")); + } + + /* "?value=" parameter is "1" or "AM" */ + else if ( atoi(value) == 1 || !strcasecmp(value, "AM") ) { + ctx->mode = AM; + _radio_set_mode (ctx->idx, ctx->mode); + json_object_object_add (jresp, "mode", json_object_new_string ("AM")); + } + + /* "?value=" parameter is "0" or "FM" */ + else if ( atoi(value) == 0 || !strcasecmp(value, "FM") ) { + ctx->mode = FM; + _radio_set_mode (ctx->idx, ctx->mode); + json_object_object_add (jresp, "mode", json_object_new_string ("FM")); + } + + afb_req_success (request, jresp, "Radio - Mode set"); +} + +static void freq (struct afb_req request) { /* AFB_SESSION_CHECK */ + + radioCtxHandleT *ctx = afb_req_context_get (request); + const char *value = afb_req_value (request, "value"); + json_object *jresp; + double freq; + char freq_str[256]; + + if (!ctx) { + afb_req_fail (request, "failed", "you must call 'init' first"); + return; + } + jresp = json_object_new_object(); + + /* no "?value=" parameter : return current state */ + if (!value || !ctx->radio) { + snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq); + json_object_object_add (jresp, "freq", json_object_new_string (freq_str)); + } + + /* "?value=" parameter, set frequency */ + else { + freq = strtod (value, NULL); + _radio_set_freq (ctx->idx, freq); + ctx->freq = (float)freq; + + snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq); + json_object_object_add (jresp, "freq", json_object_new_string (freq_str)); + } + + afb_req_success (request, jresp, "Radio - Frequency Set"); +} + +static void mute (struct afb_req request) { /* AFB_SESSION_CHECK */ + + radioCtxHandleT *ctx = afb_req_context_get (request); + const char *value = afb_req_value (request, "value"); + json_object *jresp = json_object_new_object(); + + if (!ctx) { + afb_req_fail (request, "failed", "you must call 'init' first"); + return; + } + + /* no "?value=" parameter : return current state */ + if (!value || !ctx->radio) { + ctx->mute ? + json_object_object_add (jresp, "mute", json_object_new_string ("on")) + : json_object_object_add (jresp, "mute", json_object_new_string ("off")); + } + + /* "?value=" parameter is "1" or "true" */ + else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) { + ctx->mute = 1; + _radio_set_mute (ctx->idx, ctx->mute); + json_object_object_add (jresp, "mute", json_object_new_string ("on")); + } + + /* "?value=" parameter is "0" or "false" */ + else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) { + ctx->mute = 0; + _radio_set_mute (ctx->idx, ctx->mute); + json_object_object_add (jresp, "mute", json_object_new_string ("off")); + } + + afb_req_success (request, jresp, "Radio - Mute set"); +} + +static void play (struct afb_req request) { /* AFB_SESSION_CHECK */ + + radioCtxHandleT *ctx = afb_req_context_get (request); + const char *value = afb_req_value (request, "value"); + json_object *jresp = json_object_new_object(); + + if (!ctx) { + afb_req_fail (request, "failed", "you must call 'init' first"); + return; + } + + /* no "?value=" parameter : return current state */ + if (!value || !ctx->radio) { + ctx->is_playing ? + json_object_object_add (jresp, "play", json_object_new_string ("on")) + : json_object_object_add (jresp, "play", json_object_new_string ("off")); + } + + /* "?value=" parameter is "1" or "true" */ + else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) { + /* radio playback */ + ctx->is_playing = 1; + _radio_play (ctx->idx); + json_object_object_add (jresp, "play", json_object_new_string ("on")); + } + + /* "?value=" parameter is "0" or "false" */ + else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) { + /* radio stop */ + ctx->is_playing = 0; + _radio_stop (ctx->idx); + json_object_object_add (jresp, "play", json_object_new_string ("off")); + } + + afb_req_success (request, jresp, "Radio - Play succeeded"); +} + +static void ping (struct afb_req request) { /* AFB_SESSION_NONE */ + afb_req_success (request, NULL, "Radio - Ping succeeded"); +} + + +static const struct AFB_verb_desc_v1 verbs[] = { + {"init" , AFB_SESSION_CHECK, init , "Radio API - init"}, + {"power" , AFB_SESSION_CHECK, power , "Radio API - power"}, + {"mode" , AFB_SESSION_CHECK, mode , "Radio API - mode"}, + {"freq" , AFB_SESSION_CHECK, freq , "Radio API - freq"}, + {"mute" , AFB_SESSION_CHECK, mute , "Radio API - mute"}, + {"play" , AFB_SESSION_CHECK, play , "Radio API - play"}, + {"ping" , AFB_SESSION_NONE, ping , "Radio API - ping"}, + {NULL} +}; + +static const struct AFB_plugin pluginDesc = { + .type = AFB_PLUGIN_VERSION_1, + .v1 = { + .info = "Application Framework Binder - Radio plugin", + .prefix = "radio", + .verbs = verbs + } +}; + +const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *itf) +{ + return &pluginDesc; +} diff --git a/bindings/radio/radio-api.h b/bindings/radio/radio-api.h new file mode 100644 index 00000000..9b91ec69 --- /dev/null +++ b/bindings/radio/radio-api.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RADIO_API_H +#define RADIO_API_H + +/* -------------- PLUGIN DEFINITIONS ----------------- */ + +#define MAX_RADIO 10 +typedef enum { FM, AM } Mode; + +/* structure holding one radio device with current usage status */ +typedef struct { + int idx; + char *name; + int used; +} radioDevT; + +/* global plugin handle, should store everything we may need */ +typedef struct { + radioDevT *radios[MAX_RADIO]; // pointer to existing radio + unsigned int devCount; +} pluginHandleT; + +/* private client context [will be destroyed when client leaves] */ +typedef struct { + radioDevT *radio; /* pointer to client radio */ + int idx; /* radio index within global array */ + Mode mode; /* radio mode: AM/FM */ + float freq; /* radio frequency (Mhz) */ + unsigned char mute; /* radio muted: 0(false)/1(true) */ + unsigned char is_playing; /* radio is playing: 0(false)/1(true) */ +} radioCtxHandleT; + +#endif /* RADIO_API_H */ diff --git a/bindings/radio/radio-rtlsdr.c b/bindings/radio/radio-rtlsdr.c new file mode 100644 index 00000000..7f76306e --- /dev/null +++ b/bindings/radio/radio-rtlsdr.c @@ -0,0 +1,427 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "radio-api.h" +#include "radio-rtlsdr.h" + +static void* _dongle_thread_fn (void *); +static void* _demod_thread_fn (void *); +static void* _output_thread_fn (void *); + +static unsigned int init_dev_count = 0; +static struct dev_ctx **dev_ctx = NULL; + +/* ------------- RADIO RTLSDR IMPLEMENTATION ---------------- */ + +/* --- PUBLIC FUNCTIONS --- */ + +/* Radio initialization should be done only when user start the radio and not at plugin initialization + Making this call too early would impose to restart the binder to detect a radio */ +unsigned char _radio_on (unsigned int num, radioCtxHandleT *ctx) { + + if (num >= _radio_dev_count()) + return 0; + + if (init_dev_count < _radio_dev_count()) { + init_dev_count = _radio_dev_count(); + dev_ctx = (dev_ctx_T**) realloc (dev_ctx, init_dev_count * sizeof(dev_ctx_T*)); + } + + dev_ctx[num] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T)); + dev_ctx[num]->dev = NULL; + dev_ctx[num]->mode = ctx->mode; + dev_ctx[num]->freq = ctx->freq; + dev_ctx[num]->mute = ctx->mute; + dev_ctx[num]->should_run = 0; + dev_ctx[num]->dongle = NULL; + dev_ctx[num]->demod = NULL; + dev_ctx[num]->output = NULL; + _radio_dev_init (dev_ctx[num], num); + + return 1; +} + +void _radio_off (unsigned int num) { + + if (num >= _radio_dev_count()) + return; + + if (dev_ctx[num]) { + _radio_dev_free (dev_ctx[num]); + free (dev_ctx[num]); + } + + /* free(dev_ctx); */ +} + +void _radio_set_mode (unsigned int num, Mode mode) { + if (!dev_ctx || !dev_ctx[num]) + return; + + dev_ctx[num]->mode = mode; + _radio_apply_params (dev_ctx[num]); +} + +void _radio_set_freq (unsigned int num, double freq) { + if (!dev_ctx || !dev_ctx[num]) + return; + + dev_ctx[num]->freq = (float)freq; + _radio_apply_params (dev_ctx[num]); +} + +void _radio_set_mute (unsigned int num, unsigned char mute) { + if (!dev_ctx || !dev_ctx[num]) + return; + + dev_ctx[num]->mute = mute; + _radio_apply_params (dev_ctx[num]); +} + +void _radio_play (unsigned int num) { + if (!dev_ctx || !dev_ctx[num]) + return; + + _radio_start_threads (dev_ctx[num]); +} + +void _radio_stop (unsigned int num) { + if (!dev_ctx || !dev_ctx[num]) + return; + + _radio_stop_threads (dev_ctx[num]); +} + +unsigned int _radio_dev_count () { + return rtlsdr_get_device_count(); +} + +const char* _radio_dev_name (unsigned int num) { + return rtlsdr_get_device_name (num); +} + + +/* --- LOCAL HELPER FUNCTIONS --- */ + +unsigned char _radio_dev_init (dev_ctx_T *dev_ctx, unsigned int num) { + rtlsdr_dev_t *dev = dev_ctx->dev; + + if (rtlsdr_open (&dev, num) < 0) + return 0; + + rtlsdr_set_tuner_gain_mode (dev, 0); + + if (rtlsdr_reset_buffer (dev) < 0) + return 0; + + dev_ctx->dev = dev; + + _radio_apply_params (dev_ctx); + + return 1; +} + +unsigned char _radio_dev_free (dev_ctx_T *dev_ctx) { + rtlsdr_dev_t *dev = dev_ctx->dev; + + if (rtlsdr_close (dev) < 0) + return 0; + dev = NULL; + + dev_ctx->dev = dev; + + return 1; +} + +void _radio_apply_params (dev_ctx_T *dev_ctx) { + rtlsdr_dev_t *dev = dev_ctx->dev; + Mode mode = dev_ctx->mode; + float freq = dev_ctx->freq; + int rate; + + freq *= 1000000; + rate = ((1000000 / 200000) + 1) * 200000; + + if (mode == FM) + freq += 16000; + freq += rate / 4; + + rtlsdr_set_center_freq (dev, freq); + rtlsdr_set_sample_rate (dev, rate); + + dev_ctx->dev = dev; +} + +void _radio_start_threads (dev_ctx_T *dev_ctx) { + dev_ctx->dongle = (dongle_ctx*) malloc (sizeof(dongle_ctx)); + dev_ctx->demod = (demod_ctx*) malloc (sizeof(demod_ctx)); + dev_ctx->output = (output_ctx*) malloc (sizeof(output_ctx)); + + dongle_ctx *dongle = dev_ctx->dongle; + demod_ctx *demod = dev_ctx->demod; + output_ctx *output = dev_ctx->output; + + pthread_rwlock_init (&demod->lck, NULL); + pthread_cond_init (&demod->ok, NULL); + pthread_mutex_init (&demod->ok_m, NULL); + pthread_rwlock_init (&output->lck, NULL); + pthread_cond_init (&output->ok, NULL); + pthread_mutex_init (&output->ok_m, NULL); + + dev_ctx->should_run = 1; + + /* dongle thread */ + dongle->thr_finished = 0; + pthread_create (&dongle->thr, NULL, _dongle_thread_fn, (void*)dev_ctx); + + /* demod thread */ + demod->pre_r = demod->pre_j = 0; + demod->now_r = demod->now_j = 0; + demod->index = demod->pre_index = demod->now_index = 0; + demod->thr_finished = 0; + pthread_create (&demod->thr, NULL, _demod_thread_fn, (void*)dev_ctx); + + /* output thread */ + output->thr_finished = 0; + pthread_create (&output->thr, NULL, _output_thread_fn, (void*)dev_ctx); +} + +void _radio_stop_threads (dev_ctx_T *dev_ctx) { + rtlsdr_dev_t *dev = dev_ctx->dev; + dongle_ctx *dongle = dev_ctx->dongle; + demod_ctx *demod = dev_ctx->demod; + output_ctx *output = dev_ctx->output; + + if (!dongle || !demod || !output) + return; + + /* stop each "while" loop in threads */ + dev_ctx->should_run = 0; + + rtlsdr_cancel_async (dev); + pthread_signal (&demod->ok, &demod->ok_m); + pthread_signal (&output->ok, &output->ok_m); + + while (!dongle->thr_finished || + !demod->thr_finished || + !output->thr_finished) + usleep (100000); + + pthread_join (dongle->thr, NULL); + pthread_join (demod->thr, NULL); + pthread_join (output->thr, NULL); + pthread_rwlock_destroy (&demod->lck); + pthread_cond_destroy (&demod->ok); + pthread_mutex_destroy (&demod->ok_m); + pthread_rwlock_destroy (&output->lck); + pthread_cond_destroy (&output->ok); + pthread_mutex_destroy (&output->ok_m); + + free (dongle); dev_ctx->dongle = NULL; + free (demod); dev_ctx->demod = NULL; + free (output); dev_ctx->output = NULL; +} + + /* ---- LOCAL THREADED FUNCTIONS ---- */ + +static void _rtlsdr_callback (unsigned char *buf, uint32_t len, void *ctx) { + dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx; + dongle_ctx *dongle = dev_ctx->dongle; + demod_ctx *demod = dev_ctx->demod; + unsigned char tmp; + int i; + + if (!dev_ctx->should_run) + return; + + /* rotate 90° */ + for (i = 0; i < (int)len; i += 8) { + tmp = 255 - buf[i+3]; + buf[i+3] = buf[i+2]; + buf[i+2] = tmp; + + buf[i+4] = 255 - buf[i+4]; + buf[i+5] = 255 - buf[i+5]; + + tmp = 255 - buf[i+6]; + buf[i+6] = buf[i+7]; + buf[i+7] = tmp; + } + + /* write data */ + for (i = 0; i < (int)len; i++) + dongle->buf[i] = (int16_t)buf[i] - 127; + + /* lock demod thread, write to it, unlock */ + pthread_rwlock_wrlock (&demod->lck); + memcpy (demod->buf, dongle->buf, 2 * len); + demod->buf_len = len; + pthread_rwlock_unlock (&demod->lck); + pthread_signal (&demod->ok, &demod->ok_m); +} + /**/ +static void* _dongle_thread_fn (void *ctx) { + dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx; + dongle_ctx *dongle = dev_ctx->dongle; + + rtlsdr_read_async (dev_ctx->dev, _rtlsdr_callback, dev_ctx, 0, 0); + + dongle->thr_finished = 1; + return 0; +} + +static void _lowpass_demod (void *ctx) { + demod_ctx *demod = (demod_ctx *)ctx; + int i=0, i2=0; + + while (i < demod->buf_len) { + demod->now_r += demod->buf[i]; + demod->now_j += demod->buf[i+1]; + i += 2; + demod->index++; + if (demod->index < ((1000000 / 200000) + 1)) + continue; + demod->buf[i2] = demod->now_r; + demod->buf[i2+1] = demod->now_j; + demod->index = 0; + demod->now_r = demod->now_j = 0; + i2 += 2; + } + demod->buf_len = i2; +} + /**/ +static void _lowpassreal_demod (void *ctx) { + demod_ctx *demod = (demod_ctx *)ctx; + int i=0, i2=0; + int fast = 200000; + int slow = 48000; + + while (i < demod->res_len) { + demod->now_index += demod->res[i]; + i++; + demod->pre_index += slow; + if (demod->pre_index < fast) + continue; + demod->res[i2] = (int16_t)(demod->now_index / (fast/slow)); + demod->pre_index -= fast; + demod->now_index = 0; + i2 += 1; + } + demod->res_len = i2; +} + /**/ +static void _multiply (int ar, int aj, int br, int bj, int *cr, int *cj) { + *cr = ar*br - aj*bj; + *cj = aj*br + ar*bj; +} + /**/ +static int _polar_discriminant (int ar, int aj, int br, int bj) { + int cr, cj; + double angle; + _multiply (ar, aj, br, -bj, &cr, &cj); + angle = atan2 ((double)cj, (double)cr); + return (int)(angle / 3.14159 * (1<<14)); +} + /**/ +static void _fm_demod (void *ctx) { + demod_ctx *demod = (demod_ctx *)ctx; + int16_t *buf = demod->buf; + int buf_len = demod->buf_len; + int pcm, i; + + pcm = _polar_discriminant (buf[0], buf[1], demod->pre_r, demod->pre_j); + demod->res[0] = (int16_t)pcm; + + for (i = 2; i < (buf_len-1); i += 2) { + pcm = _polar_discriminant (buf[i], buf[i+1], buf[i-2], buf[i-1]); + demod->res[i/2] = (int16_t)pcm; + } + demod->pre_r = buf[buf_len - 2]; + demod->pre_j = buf[buf_len - 1]; + demod->res_len = buf_len/2; +} + /**/ +static void _am_demod (void *ctx) { + demod_ctx *demod = (demod_ctx *)ctx; + int16_t *buf = demod->buf; + int buf_len = demod->buf_len; + int pcm, i; + + for (i = 0; i < buf_len; i += 2) { + pcm = buf[i] * buf[i]; + pcm += buf[i+1] * buf[i+1]; + demod->res[i/2] = (int16_t)sqrt(pcm); + } + demod->res_len = buf_len/2; +} + /**/ +static void* _demod_thread_fn (void *ctx) { + dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx; + demod_ctx *demod = dev_ctx->demod; + output_ctx *output = dev_ctx->output; + + while(dev_ctx->should_run) { + pthread_wait (&demod->ok, &demod->ok_m); + pthread_rwlock_wrlock (&demod->lck); + _lowpass_demod (demod); + if (dev_ctx->mode == FM) + _fm_demod (demod); + else + _am_demod (demod); + _lowpassreal_demod (demod); + pthread_rwlock_unlock (&demod->lck); + + /* lock demod thread, write to it, unlock */ + pthread_rwlock_wrlock (&output->lck); + memcpy (output->buf, demod->res, 2 * demod->res_len); + output->buf_len = demod->res_len; + pthread_rwlock_unlock (&output->lck); + pthread_signal (&output->ok, &output->ok_m); + } + + demod->thr_finished = 1; + return 0; +} + +static void* _output_thread_fn (void *ctx) { + dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx; + output_ctx *output = dev_ctx->output; + FILE *file; + + file = fopen (AUDIO_BUFFER, "wb"); + + while (dev_ctx->should_run) { + pthread_wait (&output->ok, &output->ok_m); + pthread_rwlock_rdlock (&output->lck); + if (!dev_ctx->mute && file) { + fwrite (output->buf, 2, output->buf_len, file); + fflush (file); + fseek (file, 0, SEEK_SET); + } + pthread_rwlock_unlock (&output->lck); + } + if (file) fclose(file); + unlink (AUDIO_BUFFER); + + output->thr_finished = 1; + return 0; +} diff --git a/bindings/radio/radio-rtlsdr.h b/bindings/radio/radio-rtlsdr.h new file mode 100644 index 00000000..2308f27e --- /dev/null +++ b/bindings/radio/radio-rtlsdr.h @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RADIO_RTLSDR_H +#define RADIO_RTLSDR_H + +/* -------------- RADIO RTLSDR DEFINITIONS ------------------ */ + +#include <math.h> +#include <pthread.h> +#include <rtl-sdr.h> + +#include "radio-api.h" + +#define pthread_signal(n, m) pthread_mutex_lock(m); pthread_cond_signal(n); pthread_mutex_unlock(m) +#define pthread_wait(n, m) pthread_mutex_lock(m); pthread_cond_wait(n, m); pthread_mutex_unlock(m) +#define BUF_LEN 16*16384 +#define AUDIO_BUFFER "/tmp/audio_buf" + +typedef struct dongle_ctx dongle_ctx; +typedef struct demod_ctx demod_ctx; +typedef struct output_ctx output_ctx; +typedef struct dev_ctx dev_ctx_T; + +struct dongle_ctx { + pthread_t thr; + unsigned char thr_finished; + uint16_t buf[BUF_LEN]; + uint32_t buf_len; +}; + +struct demod_ctx { + pthread_t thr; + unsigned char thr_finished; + pthread_rwlock_t lck; + pthread_cond_t ok; + pthread_mutex_t ok_m; + int pre_r, pre_j, now_r, now_j, index; + int pre_index, now_index; + int16_t buf[BUF_LEN]; + int buf_len; + int16_t res[BUF_LEN]; + int res_len; +}; + +struct output_ctx { + pthread_t thr; + unsigned char thr_finished; + pthread_rwlock_t lck; + pthread_cond_t ok; + pthread_mutex_t ok_m; + int16_t buf[BUF_LEN]; + int buf_len; +}; + +struct dev_ctx { + int used; /* TODO: radio is free ??? */ + rtlsdr_dev_t* dev; + Mode mode; + float freq; + unsigned char mute; + unsigned char should_run; + /* thread contexts */ + dongle_ctx *dongle; + demod_ctx *demod; + output_ctx *output; +}; + +unsigned int _radio_dev_count (void); +const char* _radio_dev_name (unsigned int); + +unsigned char _radio_on (unsigned int, radioCtxHandleT *); +void _radio_off (unsigned int); +void _radio_stop (unsigned int); +void _radio_play (unsigned int); +void _radio_set_mode (unsigned int, Mode); +void _radio_set_freq (unsigned int, double); +void _radio_set_mute (unsigned int, unsigned char); + +unsigned char _radio_dev_init (struct dev_ctx *, unsigned int); +unsigned char _radio_dev_free (struct dev_ctx *); +void _radio_apply_params (struct dev_ctx *); +void _radio_start_threads (struct dev_ctx *); +void _radio_stop_threads (struct dev_ctx *); + +#endif /* RADIO_RTLSDR_H */ diff --git a/bindings/samples/AuthLogin.c b/bindings/samples/AuthLogin.c new file mode 100644 index 00000000..efecf240 --- /dev/null +++ b/bindings/samples/AuthLogin.c @@ -0,0 +1,129 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Fulup Ar Foll" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#include <stdio.h> +#include <json-c/json.h> + +#include <afb/afb-plugin.h> + +// Dummy sample of Client Application Context +typedef struct { + int something; + void *whateveryouwant; +} MyClientApplicationHandle; + + +// This function is call when Client Session Context is removed +// Note: when freeCtxCB==NULL standard free/malloc is called +static void clientContextFree(void *context) { + fprintf (stderr,"Plugin[token] Closing Session\n"); + free (context); +} + +// Request Creation of new context if it does not exist +static void clientContextConnect (struct afb_req request) +{ + json_object *jresp; + + // add an application specific client context to session + afb_req_context_set(request, malloc (sizeof (MyClientApplicationHandle)), clientContextFree); + + // do something intelligent to check if we should or not update level of assurance from 0(anonymous) to 1(logged) + afb_req_session_set_LOA(request, 1); + + // Send response to UI + jresp = json_object_new_object(); + json_object_object_add(jresp, "token", json_object_new_string ("A New Token and Session Context Was Created")); + + afb_req_success(request, jresp, NULL); + +} + +// Before entering here token will be check and renew +static void clientContextRefresh (struct afb_req request) { + json_object *jresp; + + + jresp = json_object_new_object(); + json_object_object_add(jresp, "token", json_object_new_string ("Token was refreshed")); + + afb_req_success(request, jresp, NULL); +} + + +// Session token will we verified before entering here +static void clientContextCheck (struct afb_req request) { + + json_object *jresp = json_object_new_object(); + json_object_object_add(jresp, "isvalid", json_object_new_boolean (TRUE)); + + afb_req_success(request, jresp, NULL); +} + + +// Close and Free context +static void clientContextLogout (struct afb_req request) { + json_object *jresp; + + /* after this call token will be reset + * - no further access to API will be possible + * - every context from any used plugin will be freed + */ + + jresp = json_object_new_object(); + json_object_object_add(jresp, "info", json_object_new_string ("Token and all resources are released")); + + // WARNING: if you free context resource manually here do not forget to set *request.context=NULL; + afb_req_success(request, jresp, NULL); + + afb_req_session_set_LOA(request, 0); +} +// Close and Free context +static void clientGetPing (struct afb_req request) { + static int count=0; + json_object *jresp; + + jresp = json_object_new_object(); + json_object_object_add(jresp, "count", json_object_new_int (count ++)); + + afb_req_success(request, jresp, NULL); +} + + +static const struct AFB_verb_desc_v1 verbs[]= { + {"ping" , AFB_SESSION_NONE , clientGetPing ,"Ping Rest Test Service"}, + {"connect" , AFB_SESSION_LOA_EQ_0 | AFB_SESSION_RENEW, clientContextConnect,"Connect/Login Client"}, + {"refresh" , AFB_SESSION_LOA_GE_1 | AFB_SESSION_RENEW, clientContextRefresh,"Refresh Client Authentication Token"}, + {"check" , AFB_SESSION_LOA_GE_1 , clientContextCheck ,"Check Client Authentication Token"}, + {"logout" , AFB_SESSION_LOA_GE_1 | AFB_SESSION_CLOSE, clientContextLogout ,"Logout Client and Free resources"}, + {NULL} +}; + +static const struct AFB_plugin plugin_desc = { + .type = AFB_PLUGIN_VERSION_1, + .v1 = { + .info = "Application Framework Binder Authentication sample", + .prefix = "auth", + .verbs = verbs + } +}; + +const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *itf) +{ + return &plugin_desc; +} diff --git a/bindings/samples/CMakeLists.txt b/bindings/samples/CMakeLists.txt new file mode 100644 index 00000000..59a9a449 --- /dev/null +++ b/bindings/samples/CMakeLists.txt @@ -0,0 +1,63 @@ + +INCLUDE_DIRECTORIES(${include_dirs}) + +################################################## +# AuthLogin +################################################## +ADD_LIBRARY(authLogin MODULE AuthLogin.c) +SET_TARGET_PROPERTIES(authLogin PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" +) +TARGET_LINK_LIBRARIES(authLogin ${link_libraries}) +INSTALL(TARGETS authLogin + LIBRARY DESTINATION ${binding_install_dir}) + +################################################## +# DemoContext +################################################## +ADD_LIBRARY(demoContext MODULE DemoContext.c) +SET_TARGET_PROPERTIES(demoContext PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" +) +TARGET_LINK_LIBRARIES(demoContext ${link_libraries}) +INSTALL(TARGETS demoContext + LIBRARY DESTINATION ${binding_install_dir}) + +################################################## +# DemoPost +################################################## +ADD_LIBRARY(demoPost MODULE DemoPost.c) +SET_TARGET_PROPERTIES(demoPost PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" +) +TARGET_LINK_LIBRARIES(demoPost ${link_libraries}) +INSTALL(TARGETS demoPost + LIBRARY DESTINATION ${binding_install_dir}) + +################################################## +# HelloWorld +################################################## +ADD_LIBRARY(helloWorld MODULE HelloWorld.c) +SET_TARGET_PROPERTIES(helloWorld PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" +) +TARGET_LINK_LIBRARIES(helloWorld ${link_libraries}) +INSTALL(TARGETS helloWorld + LIBRARY DESTINATION ${binding_install_dir}) + +################################################## +# tic-tac-toe +################################################## +ADD_LIBRARY(tic-tac-toe MODULE tic-tac-toe.c) +SET_TARGET_PROPERTIES(tic-tac-toe PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" +) +TARGET_LINK_LIBRARIES(tic-tac-toe ${link_libraries}) +INSTALL(TARGETS tic-tac-toe + LIBRARY DESTINATION ${binding_install_dir}) + diff --git a/bindings/samples/DemoContext.c b/bindings/samples/DemoContext.c new file mode 100644 index 00000000..ef703759 --- /dev/null +++ b/bindings/samples/DemoContext.c @@ -0,0 +1,160 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Fulup Ar Foll" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#include <stdio.h> +#include <json-c/json.h> + +#include <afb/afb-plugin.h> + +typedef struct { + /* + * client context is attached a session but private to a each plugin. + * Context is passed to each API under request->context + * + * Note: + * -client context is free when a session is closed. Developer should not + * forget that even if context is private to each plugin, session is unique + * to a client. When session close, every plugin are notified to free there + * private context. + * -by default standard "free" function from libc is used to free context. + * Developer may define it own under plugin->freeCB. This call received + * FreeCtxCb(void *ClientCtx, void*PluginHandle, char*SessionUUID) if + * FreeCtxCb=(void*)-1 then context wont be free by session manager. + * -when an API use AFB_SESSION_RESET this close the session and each plugin + * will be notified to free ressources. + */ + + int count; + char *abcd; + +} MyClientContextT; + +// This function is call at session open time. Any client trying to +// call it with an already open session will be denied. +// Ex: http://localhost:1234/api/context/create?token=123456789 +static void myCreate (struct afb_req request) +{ + MyClientContextT *ctx = malloc (sizeof (MyClientContextT)); + + // store something in our plugin private client context + ctx->count = 0; + ctx->abcd = "SomeThingUseful"; + + afb_req_context_set(request, ctx, free); + afb_req_success_f(request, NULL, "SUCCESS: create client context for plugin [%s]", ctx->abcd); +} + +// This function can only be called with a valid token. Token should be renew before +// session timeout a standard renew api is avaliable at /api/token/renew this API +// can be called automatically with <token-renew> HTML5 widget. +// ex: http://localhost:1234/api/context/action?token=xxxxxx-xxxxxx-xxxxx-xxxxx-xxxxxx +static void myAction (struct afb_req request) +{ + MyClientContextT *ctx = (MyClientContextT*) afb_req_context_get(request); + + // store something in our plugin private client context + ctx->count++; + afb_req_success_f(request, NULL, "SUCCESS: plugin [%s] Check=[%d]\n", ctx->abcd, ctx->count); +} + +// After execution of this function, client session will be close and if they +// created a context [request->context != NULL] every plugins will be notified +// that they should free context resources. +// ex: http://localhost:1234/api/context/close?token=xxxxxx-xxxxxx-xxxxx-xxxxx-xxxxxx +static void myClose (struct afb_req request) +{ + MyClientContextT *ctx = (MyClientContextT*) afb_req_context_get(request); + + // store something in our plugin private client context + ctx->count++; + afb_req_success_f(request, NULL, "SUCCESS: plugin [%s] Close=[%d]\n", ctx->abcd, ctx->count); +} + +// Set the LOA +static void setLOA(struct afb_req request, unsigned loa) +{ + if (afb_req_session_set_LOA(request, loa)) + afb_req_success_f(request, NULL, "loa set to %u", loa); + else + afb_req_fail_f(request, "failed", "can't set loa to %u", loa); +} + +static void clientSetLOA0(struct afb_req request) +{ + setLOA(request, 0); +} + +static void clientSetLOA1(struct afb_req request) +{ + setLOA(request, 1); +} + +static void clientSetLOA2(struct afb_req request) +{ + setLOA(request, 2); +} + +static void clientSetLOA3(struct afb_req request) +{ + setLOA(request, 3); +} + +static void clientCheckLOA(struct afb_req request) +{ + afb_req_success(request, NULL, "LOA checked and okay"); +} + +// NOTE: this sample does not use session to keep test a basic as possible +// in real application most APIs should be protected with AFB_SESSION_CHECK +static const struct AFB_verb_desc_v1 verbs[]= { + {"create", AFB_SESSION_CREATE, myCreate , "Create a new session"}, + {"action", AFB_SESSION_CHECK , myAction , "Use Session Context"}, + {"close" , AFB_SESSION_CLOSE , myClose , "Free Context"}, + {"set_loa_0", AFB_SESSION_RENEW, clientSetLOA0 ,"Set level of assurance to 0"}, + {"set_loa_1", AFB_SESSION_RENEW, clientSetLOA1 ,"Set level of assurance to 1"}, + {"set_loa_2", AFB_SESSION_RENEW, clientSetLOA2 ,"Set level of assurance to 2"}, + {"set_loa_3", AFB_SESSION_RENEW, clientSetLOA3 ,"Set level of assurance to 3"}, + {"check_loa_ge_0", AFB_SESSION_LOA_GE_0, clientCheckLOA ,"Check whether level of assurance is greater or equal to 0"}, + {"check_loa_ge_1", AFB_SESSION_LOA_GE_1, clientCheckLOA ,"Check whether level of assurance is greater or equal to 1"}, + {"check_loa_ge_2", AFB_SESSION_LOA_GE_2, clientCheckLOA ,"Check whether level of assurance is greater or equal to 2"}, + {"check_loa_ge_3", AFB_SESSION_LOA_GE_3, clientCheckLOA ,"Check whether level of assurance is greater or equal to 3"}, + {"check_loa_le_0", AFB_SESSION_LOA_LE_0, clientCheckLOA ,"Check whether level of assurance is lesser or equal to 0"}, + {"check_loa_le_1", AFB_SESSION_LOA_LE_1, clientCheckLOA ,"Check whether level of assurance is lesser or equal to 1"}, + {"check_loa_le_2", AFB_SESSION_LOA_LE_2, clientCheckLOA ,"Check whether level of assurance is lesser or equal to 2"}, + {"check_loa_le_3", AFB_SESSION_LOA_LE_3, clientCheckLOA ,"Check whether level of assurance is lesser or equal to 3"}, + {"check_loa_eq_0", AFB_SESSION_LOA_EQ_0, clientCheckLOA ,"Check whether level of assurance is equal to 0"}, + {"check_loa_eq_1", AFB_SESSION_LOA_EQ_1, clientCheckLOA ,"Check whether level of assurance is equal to 1"}, + {"check_loa_eq_2", AFB_SESSION_LOA_EQ_2, clientCheckLOA ,"Check whether level of assurance is equal to 2"}, + {"check_loa_eq_3", AFB_SESSION_LOA_EQ_3, clientCheckLOA ,"Check whether level of assurance is equal to 3"}, + {NULL} +}; + +static const struct AFB_plugin plugin_desc = { + .type = AFB_PLUGIN_VERSION_1, + .v1 = { + .info = "Sample of Client Context Usage", + .prefix = "context", + .verbs = verbs, + } +}; + +const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *itf) +{ + return &plugin_desc; +} + diff --git a/bindings/samples/DemoPost.c b/bindings/samples/DemoPost.c new file mode 100644 index 00000000..b61b91c4 --- /dev/null +++ b/bindings/samples/DemoPost.c @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Fulup Ar Foll" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#include <stdio.h> +#include <string.h> +#include <json-c/json.h> + +#include <afb/afb-plugin.h> + + +// Sample Generic Ping Debug API +static void getPingTest(struct afb_req request) +{ + static int pingcount = 0; + json_object *query = afb_req_json(request); + + afb_req_success_f(request, query, "Ping Binder Daemon count=%d", ++pingcount); +} + +// With content-type=json data are directly avaliable in request->post->data +static void GetJsonByPost (struct afb_req request) +{ + struct afb_arg arg; + json_object* jresp; + json_object *query = afb_req_json(request); + + arg = afb_req_get(request, ""); + jresp = arg.value ? json_tokener_parse(arg.value) : NULL; + afb_req_success_f(request, jresp, "GetJsonByPost query={%s}", json_object_to_json_string(query)); +} + +// Upload a file and execute a function when upload is done +static void Uploads (struct afb_req request, const char *destination) +{ + struct afb_arg a = afb_req_get(request, "file"); + if (a.value == NULL || *a.value == 0) + afb_req_fail(request, "failed", "no file selected"); + else + afb_req_success_f(request, NULL, "uploaded file %s of path %s for destination %s", a.value, a.path, destination); +} + +// Upload a file and execute a function when upload is done +static void UploadAppli (struct afb_req request) +{ + Uploads(request, "applications"); +} + +// Simples Upload case just upload a file +static void UploadMusic (struct afb_req request) +{ + Uploads(request, "musics"); +} + +// PostForm callback is called multiple times (one or each key within form, or once per file buffer) +// When file has been fully uploaded call is call with item==NULL +static void UploadImage (struct afb_req request) +{ + Uploads(request, "images"); +} + + +// NOTE: this sample does not use session to keep test a basic as possible +// in real application upload-xxx should be protected with AFB_SESSION_CHECK +static const struct AFB_verb_desc_v1 verbs[]= { + {"ping" , AFB_SESSION_NONE , getPingTest ,"Ping Rest Test Service"}, + {"upload-json" , AFB_SESSION_NONE , GetJsonByPost ,"Demo for Json Buffer on Post"}, + {"upload-image" , AFB_SESSION_NONE , UploadImage ,"Demo for file upload"}, + {"upload-music" , AFB_SESSION_NONE , UploadMusic ,"Demo for file upload"}, + {"upload-appli" , AFB_SESSION_NONE , UploadAppli ,"Demo for file upload"}, + {NULL} +}; + +static const struct AFB_plugin plugin_desc = { + .type = AFB_PLUGIN_VERSION_1, + .v1 = { + .info = "Sample with Post Upload Files", + .prefix = "post", + .verbs = verbs + } +}; + +const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *itf) +{ + return &plugin_desc; +}; diff --git a/bindings/samples/HelloWorld.c b/bindings/samples/HelloWorld.c new file mode 100644 index 00000000..b6f49b78 --- /dev/null +++ b/bindings/samples/HelloWorld.c @@ -0,0 +1,279 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Fulup Ar Foll" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#define _GNU_SOURCE +#include <stdio.h> +#include <string.h> +#include <json-c/json.h> + +#include <afb/afb-plugin.h> + +const struct AFB_interface *interface; + +struct event +{ + struct event *next; + struct afb_event event; + char tag[1]; +}; + +static struct event *events = 0; + +/* searchs the event of tag */ +static struct event *event_get(const char *tag) +{ + struct event *e = events; + while(e && strcmp(e->tag, tag)) + e = e->next; + return e; +} + +/* deletes the event of tag */ +static int event_del(const char *tag) +{ + struct event *e, **p; + + /* check exists */ + e = event_get(tag); + if (!e) return -1; + + /* unlink */ + p = &events; + while(*p != e) p = &(*p)->next; + *p = e->next; + + /* destroys */ + afb_event_drop(e->event); + free(e); + return 0; +} + +/* creates the event of tag */ +static int event_add(const char *tag, const char *name) +{ + struct event *e; + + /* check valid tag */ + e = event_get(tag); + if (e) return -1; + + /* creation */ + e = malloc(strlen(tag) + sizeof *e); + if (!e) return -1; + strcpy(e->tag, tag); + + /* make the event */ + e->event = afb_daemon_make_event(interface->daemon, name); + if (!e->event.closure) { free(e); return -1; } + + /* link */ + e->next = events; + events = e; + return 0; +} + +static int event_subscribe(struct afb_req request, const char *tag) +{ + struct event *e; + e = event_get(tag); + return e ? afb_req_subscribe(request, e->event) : -1; +} + +static int event_unsubscribe(struct afb_req request, const char *tag) +{ + struct event *e; + e = event_get(tag); + return e ? afb_req_unsubscribe(request, e->event) : -1; +} + +static int event_push(struct json_object *args, const char *tag) +{ + struct event *e; + e = event_get(tag); + return e ? afb_event_push(e->event, json_object_get(args)) : -1; +} + +// Sample Generic Ping Debug API +static void ping(struct afb_req request, json_object *jresp, const char *tag) +{ + static int pingcount = 0; + json_object *query = afb_req_json(request); + afb_req_success_f(request, jresp, "Ping Binder Daemon tag=%s count=%d query=%s", tag, ++pingcount, json_object_to_json_string(query)); +} + +static void pingSample (struct afb_req request) +{ + ping(request, json_object_new_string ("Some String"), "pingSample"); +} + +static void pingFail (struct afb_req request) +{ + afb_req_fail(request, "failed", "Ping Binder Daemon fails"); +} + +static void pingNull (struct afb_req request) +{ + ping(request, NULL, "pingNull"); +} + +static void pingBug (struct afb_req request) +{ + ping((struct afb_req){NULL,NULL,NULL}, NULL, "pingBug"); +} + +static void pingEvent(struct afb_req request) +{ + json_object *query = afb_req_json(request); + afb_daemon_broadcast_event(interface->daemon, "event", json_object_get(query)); + ping(request, json_object_get(query), "event"); +} + + +// For samples https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/ +static void pingJson (struct afb_req request) { + json_object *jresp, *embed; + + jresp = json_object_new_object(); + json_object_object_add(jresp, "myString", json_object_new_string ("Some String")); + json_object_object_add(jresp, "myInt", json_object_new_int (1234)); + + embed = json_object_new_object(); + json_object_object_add(embed, "subObjString", json_object_new_string ("Some String")); + json_object_object_add(embed, "subObjInt", json_object_new_int (5678)); + + json_object_object_add(jresp,"eobj", embed); + + ping(request, jresp, "pingJson"); +} + +static void subcallcb (void *prequest, int iserror, json_object *object) +{ + struct afb_req request = afb_req_unstore(prequest); + if (iserror) + afb_req_fail(request, "failed", json_object_to_json_string(object)); + else + afb_req_success(request, object, NULL); + afb_req_unref(request); +} + +static void subcall (struct afb_req request) +{ + const char *api = afb_req_value(request, "api"); + const char *verb = afb_req_value(request, "verb"); + const char *args = afb_req_value(request, "args"); + json_object *object = api && verb && args ? json_tokener_parse(args) : NULL; + + if (object == NULL) + afb_req_fail(request, "failed", "bad arguments"); + else + afb_req_subcall(request, api, verb, object, subcallcb, afb_req_store(request)); +} + +static void eventadd (struct afb_req request) +{ + const char *tag = afb_req_value(request, "tag"); + const char *name = afb_req_value(request, "name"); + + if (tag == NULL || name == NULL) + afb_req_fail(request, "failed", "bad arguments"); + else if (0 != event_add(tag, name)) + afb_req_fail(request, "failed", "creation error"); + else + afb_req_success(request, NULL, NULL); +} + +static void eventdel (struct afb_req request) +{ + const char *tag = afb_req_value(request, "tag"); + + if (tag == NULL) + afb_req_fail(request, "failed", "bad arguments"); + else if (0 != event_del(tag)) + afb_req_fail(request, "failed", "deletion error"); + else + afb_req_success(request, NULL, NULL); +} + +static void eventsub (struct afb_req request) +{ + const char *tag = afb_req_value(request, "tag"); + + if (tag == NULL) + afb_req_fail(request, "failed", "bad arguments"); + else if (0 != event_subscribe(request, tag)) + afb_req_fail(request, "failed", "subscription error"); + else + afb_req_success(request, NULL, NULL); +} + +static void eventunsub (struct afb_req request) +{ + const char *tag = afb_req_value(request, "tag"); + + if (tag == NULL) + afb_req_fail(request, "failed", "bad arguments"); + else if (0 != event_unsubscribe(request, tag)) + afb_req_fail(request, "failed", "unsubscription error"); + else + afb_req_success(request, NULL, NULL); +} + +static void eventpush (struct afb_req request) +{ + const char *tag = afb_req_value(request, "tag"); + const char *data = afb_req_value(request, "data"); + json_object *object = data ? json_tokener_parse(data) : NULL; + + if (tag == NULL) + afb_req_fail(request, "failed", "bad arguments"); + else if (0 > event_push(object, tag)) + afb_req_fail(request, "failed", "push error"); + else + afb_req_success(request, NULL, NULL); +} + +// NOTE: this sample does not use session to keep test a basic as possible +// in real application most APIs should be protected with AFB_SESSION_CHECK +static const struct AFB_verb_desc_v1 verbs[]= { + {"ping" , AFB_SESSION_NONE, pingSample , "Ping Application Framework"}, + {"pingfail" , AFB_SESSION_NONE, pingFail , "Fails"}, + {"pingnull" , AFB_SESSION_NONE, pingNull , "Return NULL"}, + {"pingbug" , AFB_SESSION_NONE, pingBug , "Do a Memory Violation"}, + {"pingJson" , AFB_SESSION_NONE, pingJson , "Return a JSON object"}, + {"pingevent", AFB_SESSION_NONE, pingEvent , "Send an event"}, + {"subcall", AFB_SESSION_NONE, subcall , "Call api/verb(args)"}, + {"eventadd", AFB_SESSION_NONE, eventadd , "adds the event of 'name' for the 'tag'"}, + {"eventdel", AFB_SESSION_NONE, eventdel , "deletes the event of 'tag'"}, + {"eventsub", AFB_SESSION_NONE, eventsub , "subscribes to the event of 'tag'"}, + {"eventunsub",AFB_SESSION_NONE, eventunsub , "unsubscribes to the event of 'tag'"}, + {"eventpush", AFB_SESSION_NONE, eventpush , "pushs the event of 'tag' with the 'data'"}, + {NULL} +}; + +static const struct AFB_plugin plugin_desc = { + .type = AFB_PLUGIN_VERSION_1, + .v1 = { + .info = "Minimal Hello World Sample", + .prefix = "hello", + .verbs = verbs + } +}; + +const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *itf) +{ + interface = itf; + return &plugin_desc; +} diff --git a/bindings/samples/export.map b/bindings/samples/export.map new file mode 100644 index 00000000..0ef1ac79 --- /dev/null +++ b/bindings/samples/export.map @@ -0,0 +1 @@ +{ global: afbBindingV1Register; local: *; }; diff --git a/bindings/samples/tic-tac-toe.c b/bindings/samples/tic-tac-toe.c new file mode 100644 index 00000000..0100fc05 --- /dev/null +++ b/bindings/samples/tic-tac-toe.c @@ -0,0 +1,605 @@ +/* + * Copyright (C) 2016 "IoT.bzh" + * Author José Bollo <jose.bollo@iot.bzh> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#include <stdio.h> +#include <string.h> +#include <json-c/json.h> + +#include <afb/afb-binding.h> + +/* + * the interface to afb-daemon + */ +const struct afb_binding_interface *afbitf; + +/* + * definition of waiters + */ +struct waiter +{ + struct waiter *next; + struct afb_req req; +}; + +/* + * definition of a board + */ +struct board +{ + struct board *next; + int use_count; + int moves; + int history[9]; + int id; + int level; + char board[9]; + struct waiter *waiters; +}; + +/* + * list of boards + */ +static struct board *all_boards; + +/* + * Searchs a board having the 'id'. + * Returns it if found or NULL otherwise. + */ +static struct board *search_board(int id) +{ + struct board *board = all_boards; + while (board != NULL && board->id != id) + board = board->next; + return board; +} + +/* + * Creates a new board and returns it. + */ +static struct board *get_new_board() +{ + /* allocation */ + struct board *board = calloc(1, sizeof *board); + + /* initialisation */ + memset(board->board, ' ', sizeof board->board); + board->use_count = 1; + board->level = 1; + board->moves = 0; + do { + board->id = (rand() >> 2) % 1000; + } while(board->id == 0 || search_board(board->id) != NULL); + + /* link */ + board->next = all_boards; + all_boards = board; + return board; +} + +/* + * Release a board + */ +static void release_board(struct board *board) +{ + /* decrease the reference count ... */ + if (--board->use_count == 0) { + /* ... no more use */ + /* unlink from the list of boards */ + struct board **prv = &all_boards; + while (*prv != NULL && *prv != board) + prv = &(*prv)->next; + if (*prv != NULL) + *prv = board->next; + /* release the used memory */ + free(board); + } +} + +/* + * Checks who wins + * Returns zero if there is no winner + * Returns the char of the winner if a player won + */ +static char winner(const char b[9]) +{ + int i; + char c; + + /* check diagonals */ + c = b[4]; + if (c != ' ') { + if (b[0] == c && b[8] == c) + return c; + if (b[2] == c && b[6] == c) + return c; + } + + /* check lines */ + for (i = 0 ; i <= 6 ; i += 3) { + c = b[i]; + if (c != ' ' && b[i+1] == c && b[i+2] == c) + return c; + } + + /* check columns */ + for (i = 0 ; i <= 2 ; i++) { + c = b[i]; + if (c != ' ' && b[i+3] == c && b[i+6] == c) + return c; + } + + return 0; +} + +/* get the color (X or 0) of the move of index 'move' */ +static char color(int move) +{ + return (move & 1) == 0 ? 'X' : '0'; +} + +/* adds the move to the board */ +static void add_move(struct board *board, int index) +{ + int imove = board->moves++; + board->history[imove] = index; + board->board[index] = color(imove); +} + +/* get a random possible move index from the board described by 'b' */ +static int get_random_move(char b[9]) +{ + int index = rand() % 9; + while (b[index] != ' ') + index = (index + 1) % 9; + return index; +} + +/* + * Scores the position described by 'b' + * for the player of color 'c' using an analysis of 'depth'. + * Returns 1 if player 'c' will win. + * Returns -1 if opponent of player 'c' will win. + * returns 0 otherwise. + */ +static int score_position(char b[9], char c, int depth) +{ + int i, t, r; + + /* check if winner */ + if (winner(b) == c) + return 1; + + /* when depth of analysis is reached return unknown case */ + if (--depth == 0) + return 0; + + /* switch to the opponent */ + c = (char)('O' + 'X' - c); + + /* inspect opponent moves */ + r = 1; + for (i = 0 ; i < 9 ; i++) { + if (b[i] == ' ') { + b[i] = c; + t = score_position(b, c, depth); + b[i] = ' '; + if (t > 0) + return -1; /* opponent will win */ + + if (t == 0) + r = 0; /* something not clear */ + } + } + return r; +} + +/* get one move: return the computed index of the move */ +static int get_move(struct board *board) +{ + int index, depth, t, f; + char c; + char b[9]; + + /* compute the depth */ + depth = board->level - 1; + if (board->moves + depth > 9) + depth = 9 - board->moves; + + /* case of null depth */ + if (depth == 0) + return get_random_move(board->board); + + /* depth and more */ + memcpy(b, board->board, 9); + c = color(board->moves); + f = 0; + for (index = 0 ; index < 9 ; index++) { + if (board->board[index] == ' ') { + board->board[index] = c; + t = score_position(board->board, c, depth); + board->board[index] = ' '; + if (t > 0) + return index; + if (t < 0) + b[index] = '+'; + else + f = 1; + } + } + return get_random_move(f ? b : board->board); +} + +/* + * get the board description + */ +static struct json_object *describe(struct board *board) +{ + int i; + char w; + struct json_object *resu, *arr; + + resu = json_object_new_object(); + + json_object_object_add(resu, "boardid", json_object_new_int(board->id)); + json_object_object_add(resu, "level", json_object_new_int(board->level)); + + arr = json_object_new_array(); + json_object_object_add(resu, "board", arr); + for (i = 0 ; i < 9 ; i++) + json_object_array_add(arr, + json_object_new_string_len(&board->board[i], 1)); + + arr = json_object_new_array(); + json_object_object_add(resu, "history", arr); + for (i = 0 ; i < board->moves ; i++) + json_object_array_add(arr, json_object_new_int(board->history[i])); + + w = winner(board->board); + if (w) + json_object_object_add(resu, "winner", json_object_new_string_len(&w, 1)); + else if (board->moves == 9) + json_object_object_add(resu, "winner", json_object_new_string("none")); + + return resu; +} + +/* + * signals a change of the board + */ +static void changed(struct board *board, const char *reason) +{ + struct waiter *waiter, *next; + struct json_object *description; + + /* get the description */ + description = describe(board); + + waiter = board->waiters; + board->waiters = NULL; + while (waiter != NULL) { + next = waiter->next; + afb_req_success(waiter->req, json_object_get(description), reason); + afb_req_unref(waiter->req); + free(waiter); + waiter = next; + } + + afb_daemon_broadcast_event(afbitf->daemon, reason, description); +} + +/* + * retrieves the board of the request + */ +static inline struct board *board_of_req(struct afb_req req) +{ + return afb_req_context(req, (void*)get_new_board, (void*)release_board); +} + +/* + * start a new game + */ +static void new(struct afb_req req) +{ + struct board *board; + + /* retrieves the context for the session */ + board = board_of_req(req); + INFO(afbitf, "method 'new' called for boardid %d", board->id); + + /* reset the game */ + memset(board->board, ' ', sizeof board->board); + board->moves = 0; + + /* replies */ + afb_req_success(req, NULL, NULL); + + /* signal change */ + changed(board, "new"); +} + +/* + * get the board + */ +static void board(struct afb_req req) +{ + struct board *board; + struct json_object *description; + + /* retrieves the context for the session */ + board = board_of_req(req); + INFO(afbitf, "method 'board' called for boardid %d", board->id); + + /* describe the board */ + description = describe(board); + + /* send the board's description */ + afb_req_success(req, description, NULL); +} + +/* + * move a piece + */ +static void move(struct afb_req req) +{ + struct board *board; + int i; + const char *index; + + /* retrieves the context for the session */ + board = board_of_req(req); + INFO(afbitf, "method 'move' called for boardid %d", board->id); + + /* retrieves the arguments of the move */ + index = afb_req_value(req, "index"); + i = index == NULL ? -1 : atoi(index); + + /* checks validity of arguments */ + if (i < 0 || i > 8) { + WARNING(afbitf, "can't move to %s: %s", index?:"?", index?"wrong value":"not set"); + afb_req_fail(req, "error", "bad request"); + return; + } + + /* checks validity of the state */ + if (winner(board->board) != 0) { + WARNING(afbitf, "can't move to %s: game is terminated", index); + afb_req_fail(req, "error", "game terminated"); + return; + } + + /* checks validity of the move */ + if (board->board[i] != ' ') { + WARNING(afbitf, "can't move to %s: room occupied", index); + afb_req_fail(req, "error", "occupied"); + return; + } + + /* applies the move */ + INFO(afbitf, "method 'move' for boardid %d, index=%s", board->id, index); + add_move(board, i); + + /* replies */ + afb_req_success(req, NULL, NULL); + + /* signals change */ + changed(board, "move"); +} + +/* + * set the level + */ +static void level(struct afb_req req) +{ + struct board *board; + int l; + const char *level; + + /* retrieves the context for the session */ + board = board_of_req(req); + INFO(afbitf, "method 'level' called for boardid %d", board->id); + + /* retrieves the arguments */ + level = afb_req_value(req, "level"); + l = level == NULL ? -1 : atoi(level); + + /* check validity of arguments */ + if (l < 1 || l > 10) { + WARNING(afbitf, "can't set level to %s: %s", level?:"?", level?"wrong value":"not set"); + afb_req_fail(req, "error", "bad request"); + return; + } + + /* set the level */ + INFO(afbitf, "method 'level' for boardid %d, level=%d", board->id, l); + board->level = l; + + /* replies */ + afb_req_success(req, NULL, NULL); + + /* signals change */ + changed(board, "level"); +} + +/* + * Join a board + */ +static void join(struct afb_req req) +{ + struct board *board, *new_board; + const char *id; + + /* retrieves the context for the session */ + board = board_of_req(req); + INFO(afbitf, "method 'join' called for boardid %d", board->id); + + /* retrieves the arguments */ + id = afb_req_value(req, "boardid"); + if (id == NULL) + goto bad_request; + + /* none is a special id for joining a new session */ + if (strcmp(id, "none") == 0) { + new_board = get_new_board(); + goto success; + } + + /* searchs the board to join */ + new_board = search_board(atoi(id)); + if (new_board == NULL) + goto bad_request; + + /* + * joining its board is stupid but possible + * however when called with the same stored pointer + * afb_req_context_set will not call the release + * function 'release_board'. So the use_count MUST not + * be incremented. + */ + if (new_board != board) + new_board->use_count++; + +success: + /* set the new board (and leaves the previous one) */ + afb_req_context_set(req, new_board, (void*)release_board); + + /* replies */ + afb_req_success(req, NULL, NULL); + return; + +bad_request: + WARNING(afbitf, "can't join boardid %s: %s", id ? : "?", !id ? "no boardid" : atoi(id) ? "not found" : "bad boardid"); + afb_req_fail(req, "error", "bad request"); + return; +} + +/* + * Undo the last move + */ +static void undo(struct afb_req req) +{ + struct board *board; + int i; + + /* retrieves the context for the session */ + board = board_of_req(req); + INFO(afbitf, "method 'undo' called for boardid %d", board->id); + + /* checks the state */ + if (board->moves == 0) { + WARNING(afbitf, "can't undo"); + afb_req_fail(req, "error", "bad request"); + return; + } + + /* undo the last move */ + i = board->history[--board->moves]; + board->board[i] = ' '; + + /* replies */ + afb_req_success(req, NULL, NULL); + + /* signals change */ + changed(board, "undo"); +} + +/* + * computer plays + */ +static void play(struct afb_req req) +{ + struct board *board; + int index; + + /* retrieves the context for the session */ + board = board_of_req(req); + INFO(afbitf, "method 'play' called for boardid %d", board->id); + + /* checks validity of the state */ + if (winner(board->board) != 0 || board->moves == 9) { + WARNING(afbitf, "can't play: game terminated (%s)", winner(board->board) ? "has winner" : "no room left"); + afb_req_fail(req, "error", "game terminated"); + return; + } + + /* gets the move and plays it */ + index = get_move(board); + add_move(board, index); + + /* replies */ + afb_req_success(req, describe(board), NULL); + + /* signals change */ + changed(board, "play"); +} + +static void wait(struct afb_req req) +{ + struct board *board; + struct waiter *waiter; + + /* retrieves the context for the session */ + board = board_of_req(req); + INFO(afbitf, "method 'wait' called for boardid %d", board->id); + + /* creates the waiter and enqueues it */ + waiter = calloc(1, sizeof *waiter); + waiter->req = req; + waiter->next = board->waiters; + afb_req_addref(req); + board->waiters = waiter; +} + +/* + * array of the verbs exported to afb-daemon + */ +static const struct afb_verb_desc_v1 binding_verbs[] = { + /* VERB'S NAME SESSION MANAGEMENT FUNCTION TO CALL SHORT DESCRIPTION */ + { .name= "new", .session= AFB_SESSION_NONE, .callback= new, .info= "Starts a new game" }, + { .name= "play", .session= AFB_SESSION_NONE, .callback= play, .info= "Asks the server to play" }, + { .name= "move", .session= AFB_SESSION_NONE, .callback= move, .info= "Tells the client move" }, + { .name= "board", .session= AFB_SESSION_NONE, .callback= board, .info= "Get the current board" }, + { .name= "level", .session= AFB_SESSION_NONE, .callback= level, .info= "Set the server level" }, + { .name= "join", .session= AFB_SESSION_CHECK,.callback= join, .info= "Join a board" }, + { .name= "undo", .session= AFB_SESSION_NONE, .callback= undo, .info= "Undo the last move" }, + { .name= "wait", .session= AFB_SESSION_NONE, .callback= wait, .info= "Wait for a change" }, + { .name= NULL } /* marker for end of the array */ +}; + +/* + * description of the binding for afb-daemon + */ +static const struct afb_binding binding_description = +{ + /* description conforms to VERSION 1 */ + .type= AFB_BINDING_VERSION_1, + .v1= { /* fills the v1 field of the union when AFB_BINDING_VERSION_1 */ + .prefix= "tictactoe", /* the API name (or binding name or prefix) */ + .info= "Sample tac-tac-toe game", /* short description of of the binding */ + .verbs = binding_verbs /* the array describing the verbs of the API */ + } +}; + +/* + * activation function for registering the binding called by afb-daemon + */ +const struct afb_binding *afbBindingV1Register(const struct afb_binding_interface *itf) +{ + afbitf = itf; // records the interface for accessing afb-daemon + return &binding_description; // returns the description of the binding +} + |