blob: 494f2cbee17c3ab894920960f5b721c7b41eb614 [file] [log] [blame]
/*
* Copyright (c) 2021, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include"PalUsecaseTest.h"
#include <errno.h>
#include <string.h>
static struct pal_stream_attributes *stream_attributes;
static struct pal_device *pal_devices;
static pal_stream_handle_t *pal_stream;
int32_t OpenAndStartUsecase(int usecase_type) {
pal_stream_type_t usecase = (pal_stream_type_t)usecase_type;
int32_t status = 0;
fprintf(stdout, "Enter OpenAndStartUsecase\n");
switch (usecase) {
case PAL_STREAM_ULTRASOUND:
status = setup_usecase_ultrasound();
if (status) {
fprintf(stdout, "Error:Failed to Start UPD\n");
goto exit;
}
fprintf(stdout, "Stream started succefully\n");
break;
default :
fprintf(stdout, "unkown uasecase\n");
status = -EINVAL;
break;
}
exit:
fprintf(stdout, "Exit OpenAndStartUsecase\n");
return status;
}
int32_t StopAndCloseUsecase()
{
int32_t status = 0;
fprintf(stdout, "Enter StopAndCloseUsecase\n");
if (pal_stream) {
status = pal_stream_stop(pal_stream);
if (status) {
fprintf(stdout, "pal_stream_stop failed\n");
}
status = pal_stream_close(pal_stream);
if (status) {
fprintf(stdout, "pal_stream_close failed\n");
}
pal_stream = NULL;
if (stream_attributes)
free(stream_attributes);
if (pal_devices)
free(pal_devices);
}
fprintf(stdout, "Exit StopAndCloseUltrasound\n");
return status;
}
int32_t setup_usecase_ultrasound()
{
int32_t status = 0;
pal_param_payload *param_payload = NULL;
pal_param_upd_event_detection_t payload;
int32_t no_of_devices = 2;
//setting stream attributes
stream_attributes = (struct pal_stream_attributes *)
calloc (1, sizeof(struct pal_stream_attributes));
if (!stream_attributes)
goto exit;
stream_attributes->type = PAL_STREAM_ULTRASOUND;
stream_attributes->direction = PAL_AUDIO_INPUT_OUTPUT;
// setting device attriutes
// device attributes for UPD will be set based on BE used.
pal_devices = (struct pal_device *) calloc(no_of_devices, sizeof(struct pal_device));
status = pal_stream_open(stream_attributes, no_of_devices, pal_devices, 0, NULL,
(pal_stream_callback)&HandleCallbackForUPD, 0, &pal_stream);
if (status) {
fprintf(stdout, "Error:Failed to open UPD stream\n");
goto exit;
}
fprintf(stdout, "Stream Opened succesfully\n");
param_payload = (pal_param_payload *) calloc (1,
sizeof(pal_param_payload) +
sizeof(pal_param_upd_event_detection_t));
if (!param_payload)
goto exit;
payload.register_status = 1;
param_payload->payload_size = sizeof(pal_param_upd_event_detection_t);
memcpy(param_payload->payload, &payload, param_payload->payload_size);
status = pal_stream_set_param(pal_stream, PAL_PARAM_ID_UPD_REGISTER_FOR_EVENTS, param_payload);
if (status) {
fprintf(stdout, "setParams failed");
goto close_stream;
}
status = pal_stream_start(pal_stream);
if (status) {
fprintf(stdout, "Error:Failed to Start UPD");
goto close_stream;
}
goto exit;
close_stream:
pal_stream_close(pal_stream);
pal_stream = NULL;
exit:
if (param_payload)
free(param_payload);
if (stream_attributes)
free(stream_attributes);
return status;
}
static int32_t HandleCallbackForUPD(pal_stream_handle_t *stream_handle,
uint32_t event_id, uint32_t *event_data,
uint32_t event_size, uint64_t cookie)
{
int32_t status = 0;
if (event_id == EVENT_ID_GENERIC_US_DETECTION) {
if (*event_data == 1)
fprintf(stdout, "Event Detected : Near event received\n");
else if (*event_data == 2)
fprintf(stdout, "Event Detected : Far event received\n");
else
fprintf(stdout, "Event Detected : Invalid event %d\n", *event_data);
}
return status;
}