aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/4a-internals-hal/4a-internals-hal-alsacore-link.c154
-rw-r--r--src/4a-internals-hal/4a-internals-hal-alsacore-link.h37
-rw-r--r--src/4a-internals-hal/4a-internals-hal-cb.c84
3 files changed, 199 insertions, 76 deletions
diff --git a/src/4a-internals-hal/4a-internals-hal-alsacore-link.c b/src/4a-internals-hal/4a-internals-hal-alsacore-link.c
index 7b2fd5f..99b50a8 100644
--- a/src/4a-internals-hal/4a-internals-hal-alsacore-link.c
+++ b/src/4a-internals-hal/4a-internals-hal-alsacore-link.c
@@ -66,6 +66,160 @@ snd_ctl_elem_type_t InternalHalMapsAlsaTypeToEnum(const char *label)
}
/*******************************************************************************
+ * Internals HAL - Alsacore events handler function *
+ ******************************************************************************/
+
+enum aslacoreEventType InternalHalGetAlsaCoreEventType(const char *evtLabel)
+{
+ if(! evtLabel)
+ return ALSACORE_UNRECOGNIZED_EVENT;
+
+ if(! strncmp(&evtLabel[ALSACORE_EVENT_NAME_BEGIN],
+ ALSACORE_CARD_CONTROL_EVENT_NAME_PREFIX,
+ ALSACORE_CARD_CONTROL_EVENT_NAME_PREFIX_LENGTH))
+ return ALSACORE_EVENT_CARD_CONTROL_CHANGE;
+
+ return ALSACORE_UNRECOGNIZED_EVENT;
+}
+
+int InternalHalHandleAlsaCoreCardControlEvent(afb_api_t apiHandle,
+ const char *evtLabel,
+ json_object *eventJ,
+ struct HalData *currentHalData)
+{
+ int numid, cardidx;
+
+ json_object *valuesJ, *normalizedValuesJ;
+
+ CtlSourceT source;
+
+ struct InternalHalAlsaMap *currentHalMapControl;
+
+ if(! apiHandle || ! evtLabel || ! eventJ || ! currentHalData) {
+ AFB_API_ERROR(apiHandle, "Invalid argument(s)");
+ return -1;
+ }
+
+ if(sscanf(&evtLabel[ALSACORE_CARD_CONTROL_EVENT_BEGIN], "%d", &cardidx) != 1) {
+ AFB_API_ERROR(apiHandle,
+ "Did not succeed to get ALSA card number of the modified control : evtname=%s [msg=%s]",
+ evtLabel,
+ json_object_get_string(eventJ));
+ return -2;
+ }
+
+ if(wrap_json_unpack(eventJ, "{s:i s:o}", "id", &numid, "val", &valuesJ)) {
+ AFB_API_ERROR(apiHandle,
+ "Invalid alsacore modified control event received label=%s value=%s",
+ evtLabel,
+ json_object_get_string(eventJ));
+ return -3;
+ }
+
+ if(cds_list_empty(&currentHalData->internalHalData->halMapListHead)) {
+ AFB_API_ERROR(apiHandle,
+ "No halmap data is available, cannot handle alsacore event label=%s value=%s",
+ evtLabel,
+ json_object_get_string(eventJ));
+ return -4;
+ }
+
+ // Search for corresponding numid in ALSA controls list, if found, launch callback (if available)
+ cds_list_for_each_entry(currentHalMapControl, &currentHalData->internalHalData->halMapListHead, node) {
+ if(currentHalMapControl->cardNb != cardidx ||
+ ! currentHalMapControl->ctl.alsaCtlProperties ||
+ currentHalMapControl->ctl.numid != numid)
+ continue;
+
+ if(currentHalMapControl->action) {
+ memset(&source, 0, sizeof(CtlSourceT));
+ source.uid = currentHalMapControl->action->uid;
+ source.api = currentHalMapControl->action->api;
+
+ ActionExecOne(&source, currentHalMapControl->action, valuesJ);
+ }
+ else if(currentHalMapControl->actionJ) {
+ AFB_API_WARNING(apiHandle,
+ "The alsa control id '%i' is corresponding to a known control which "
+ "has a registered action, but action is not ready to be executed",
+ numid);
+ }
+ else {
+ AFB_API_NOTICE(apiHandle,
+ "The alsa control id '%i' is corresponding to a known control "
+ "but without any action registered",
+ numid);
+ }
+
+ if(! currentHalMapControl->alsaControlEvent ||
+ InternalHalConvertJsonValues(apiHandle,
+ currentHalMapControl->ctl.alsaCtlProperties,
+ valuesJ,
+ &normalizedValuesJ,
+ CONVERSION_ALSACORE_TO_NORMALIZED) ||
+ (afb_event_push(currentHalMapControl->alsaControlEvent, normalizedValuesJ) < 0)) {
+ AFB_API_ERROR(apiHandle,
+ "Couldn't generate an event for known halmap %s (alsa control id %i)",
+ currentHalMapControl->uid,
+ currentHalMapControl->ctl.numid);
+ }
+
+ return 0;
+ }
+
+ AFB_API_INFO(apiHandle,
+ "Alsacore event with an unrecognized numid: %i, evtname=%s [msg=%s]",
+ numid,
+ evtLabel,
+ json_object_get_string(eventJ));
+
+ return 1;
+
+}
+
+int InternalHalHandleAlsaCoreEvents(afb_api_t apiHandle,
+ const char *evtLabel,
+ json_object *eventJ,
+ struct HalData *currentHalData)
+{
+ int returned;
+
+ if(! apiHandle || ! evtLabel || ! eventJ || ! currentHalData) {
+ AFB_API_ERROR(apiHandle, "Invalid argument(s)");
+ return -1;
+ }
+
+ switch(InternalHalGetAlsaCoreEventType(evtLabel)) {
+ case ALSACORE_EVENT_CARD_CONTROL_CHANGE:
+ returned = InternalHalHandleAlsaCoreCardControlEvent(apiHandle, evtLabel, eventJ, currentHalData);
+ if(returned < 0) {
+ AFB_API_ERROR(apiHandle,
+ "Error %i caught when tried to handle alsacore card control event evtname=%s [msg=%s]",
+ returned,
+ evtLabel,
+ json_object_get_string(eventJ));
+ return -2;
+ }
+ else if(returned > 0) {
+ AFB_API_INFO(apiHandle,
+ "Warning %i raised when tried to handle alsacore card control event evtname=%s [msg=%s]",
+ returned,
+ evtLabel,
+ json_object_get_string(eventJ));
+ }
+ return 0;
+
+ case ALSACORE_UNRECOGNIZED_EVENT:
+ default:
+ AFB_API_NOTICE(apiHandle,
+ "Alsacore event with an unrecognized evtname=%s [msg=%s]",
+ evtLabel,
+ json_object_get_string(eventJ));
+ return 1;
+ }
+}
+
+/*******************************************************************************
* Internals HAL - Alsacore calls funtions *
******************************************************************************/
diff --git a/src/4a-internals-hal/4a-internals-hal-alsacore-link.h b/src/4a-internals-hal/4a-internals-hal-alsacore-link.h
index 84a096a..0694a81 100644
--- a/src/4a-internals-hal/4a-internals-hal-alsacore-link.h
+++ b/src/4a-internals-hal/4a-internals-hal-alsacore-link.h
@@ -19,6 +19,7 @@
#define _INTERNALS_HAL_ALSACORE_LINK_INCLUDE_
#include <stdio.h>
+#include <string.h>
#include <wrap-json.h>
@@ -29,17 +30,31 @@
#include <ctl-config.h>
#include "4a-hal-utilities-alsa-data.h"
+#include "4a-hal-utilities-data.h"
-#define ALSACORE_API "alsacore"
+#define ALSACORE_API "alsacore"
+#define ALSACORE_API_NAME_LENGTH strlen(ALSACORE_API)
-#define ALSACORE_SUBSCRIBE_VERB "subscribe"
-#define ALSACORE_UNSUBSCRIBE_VERB "unsubscribe"
-#define ALSACORE_GETINFO_VERB "infoget"
-#define ALSACORE_CTLGET_VERB "ctlget"
-#define ALSACORE_CTLSET_VERB "ctlset"
-#define ALSACORE_ADDCTL_VERB "addcustomctl"
+#define ALSACORE_SUBSCRIBE_VERB "subscribe"
+#define ALSACORE_UNSUBSCRIBE_VERB "unsubscribe"
+#define ALSACORE_GETINFO_VERB "infoget"
+#define ALSACORE_CTLGET_VERB "ctlget"
+#define ALSACORE_CTLSET_VERB "ctlset"
+#define ALSACORE_ADDCTL_VERB "addcustomctl"
-#define ALSACORE_CARD_CONTROL_EVENT_NAME "controls"
+#define ALSACORE_CARD_CONTROL_EVENT_NAME "controls"
+
+#define ALSACORE_EVENT_NAME_BEGIN (ALSACORE_API_NAME_LENGTH + 1)
+#define ALSACORE_CARD_CONTROL_EVENT_NAME_PREFIX "hw:"
+#define ALSACORE_CARD_CONTROL_EVENT_NAME_PREFIX_LENGTH strlen(ALSACORE_CARD_CONTROL_EVENT_NAME_PREFIX)
+#define ALSACORE_CARD_CONTROL_EVENT_BEGIN (ALSACORE_EVENT_NAME_BEGIN + ALSACORE_CARD_CONTROL_EVENT_NAME_PREFIX_LENGTH)
+
+// Enum for handled alsacore events
+
+enum aslacoreEventType {
+ ALSACORE_EVENT_CARD_CONTROL_CHANGE = 0,
+ ALSACORE_UNRECOGNIZED_EVENT = 1
+};
// Enum for the type of subscription/unsubscription to do with alsacore
enum AlsacoreSubscribeUnsubscribeType {
@@ -50,6 +65,12 @@ enum AlsacoreSubscribeUnsubscribeType {
// Alsa control types map from string function
snd_ctl_elem_type_t InternalHalMapsAlsaTypeToEnum(const char *label);
+// Internals HAL alsacore events handler function
+int InternalHalHandleAlsaCoreEvents(afb_api_t apiHandle,
+ const char *evtLabel,
+ json_object *eventJ,
+ struct HalData *currentHalData);
+
// Internals HAL alsacore calls funtions
int InternalHalGetCardInfo(afb_api_t apiHandle, json_object *requestJ, json_object **responseJ);
int InternalHalSubscribeToAlsacoreCardEvent(afb_api_t apiHandle, char *cardId);
diff --git a/src/4a-internals-hal/4a-internals-hal-cb.c b/src/4a-internals-hal/4a-internals-hal-cb.c
index 34fbe74..65ff96d 100644
--- a/src/4a-internals-hal/4a-internals-hal-cb.c
+++ b/src/4a-internals-hal/4a-internals-hal-cb.c
@@ -37,15 +37,11 @@
void InternalHalDispatchApiEvent(afb_api_t apiHandle, const char *evtLabel, json_object *eventJ)
{
- int numid, idx = 0, cardidx;
+ int error;
CtlConfigT *ctrlConfig;
- CtlSourceT source;
struct HalData *currentHalData;
- struct InternalHalAlsaMap *currentHalMapControl;
-
- json_object *valuesJ, *normalizedValuesJ;
AFB_API_DEBUG(apiHandle, "Evtname=%s [msg=%s]", evtLabel, json_object_get_string(eventJ));
@@ -61,79 +57,31 @@ void InternalHalDispatchApiEvent(afb_api_t apiHandle, const char *evtLabel, json
return;
}
- // Extract sound card index from event
- while(evtLabel[idx] != '\0' && evtLabel[idx] != ':')
- idx++;
-
- if(evtLabel[idx] != '\0' &&
- (sscanf(&evtLabel[idx + 1], "%d", &cardidx) == 1)) {
- if(wrap_json_unpack(eventJ, "{s:i s:o}", "id", &numid, "val", &valuesJ)) {
+ if((strlen(evtLabel) > ALSACORE_API_NAME_LENGTH) &&
+ ! strncmp(evtLabel, ALSACORE_API, ALSACORE_API_NAME_LENGTH)) {
+ error = InternalHalHandleAlsaCoreEvents(apiHandle, evtLabel, eventJ, currentHalData);
+ if(error < 0) {
AFB_API_ERROR(apiHandle,
- "Invalid alsacore event received label=%s value=%s",
+ "Error %i caught when tried to handle alsacore event evtname=%s [msg=%s]",
+ error,
evtLabel,
json_object_get_string(eventJ));
return;
}
-
- if(cds_list_empty(&currentHalData->internalHalData->halMapListHead)) {
- AFB_API_ERROR(apiHandle, "No halmap data is available, cannot handle alsacore event");
- return;
+ else if(error > 0) {
+ AFB_API_INFO(apiHandle,
+ "Warning %i raised when tried to handle alsacore event evtname=%s [msg=%s]",
+ error,
+ evtLabel,
+ json_object_get_string(eventJ));
}
-
- // Search for corresponding numid in ALSA controls list, if found, launch callback (if available)
- cds_list_for_each_entry(currentHalMapControl, &currentHalData->internalHalData->halMapListHead, node) {
- if(currentHalMapControl->cardNb == cardidx &&
- currentHalMapControl->ctl.alsaCtlProperties &&
- currentHalMapControl->ctl.numid == numid) {
- if(currentHalMapControl->action) {
- memset(&source, 0, sizeof(CtlSourceT));
- source.uid = currentHalMapControl->action->uid;
- source.api = currentHalMapControl->action->api;
-
- (void) ActionExecOne(&source, currentHalMapControl->action, valuesJ);
- }
- else if(currentHalMapControl->actionJ) {
- AFB_API_WARNING(apiHandle,
- "The alsa control id '%i' is corresponding to a known control which "
- "has a registered action, but action is not ready to be executed",
- numid);
- return;
- }
- else {
- AFB_API_NOTICE(apiHandle,
- "The alsa control id '%i' is corresponding to a known control "
- "but without any action registered",
- numid);
- }
-
- if(! currentHalMapControl->alsaControlEvent ||
- InternalHalConvertJsonValues(apiHandle,
- currentHalMapControl->ctl.alsaCtlProperties,
- valuesJ,
- &normalizedValuesJ,
- CONVERSION_ALSACORE_TO_NORMALIZED) ||
- (afb_event_push(currentHalMapControl->alsaControlEvent, normalizedValuesJ) < 0)) {
- AFB_API_ERROR(apiHandle,
- "Couldn't generate an event for known halmap %s (alsa control id %i)",
- currentHalMapControl->uid,
- currentHalMapControl->ctl.numid);
- }
-
- return;
- }
+ else {
+ return;
}
-
- AFB_API_WARNING(apiHandle,
- "Alsacore event with an unrecognized numid: %i, evtname=%s [msg=%s]",
- numid,
- evtLabel,
- json_object_get_string(eventJ));
-
- return;
}
AFB_API_INFO(apiHandle,
- "Not an alsacore event '%s' [msg=%s]",
+ "Not an internally handled event '%s' [msg=%s]",
evtLabel,
json_object_get_string(eventJ));