| #include <stdio.h> |
| #include <stdlib.h> |
| #include <libubox/blobmsg_json.h> |
| #include "libubus.h" |
| |
| #include "audio_if_parameter.h" |
| //#include <include/audio_if.h> |
| //#include <include/audio_if_types.h> |
| //#include <include/audio_if_ubus.h> |
| |
| #define LOG(fmt, args...) \ |
| do{ \ |
| printf("%s#%d: " fmt "\n", __FUNCTION__, __LINE__, ##args); \ |
| } while(0) |
| |
| //User task |
| static pthread_t APP_MainLoopTask; |
| |
| /**********************************************************************\ |
| * APP Global Variables for uBus |
| \**********************************************************************/ |
| #define AUDIO_UBUS_ID "audio_if" |
| #define AUDIO_UBUS_AUDIO_GAIN_SET "audio_gain_set" |
| |
| static struct ubus_context *APP_ctx = NULL; |
| static uint32_t APP_audio_request_id; |
| static struct blob_buf audio_cm_b; |
| |
| |
| /**********************************************************************\ |
| * Function: APP_uBusInit |
| * Description: init UBUS context |
| * Returns: 0 on success |
| \**********************************************************************/ |
| static int APP_uBusInit(void) |
| { |
| int rc = 0; |
| |
| uloop_init(); |
| APP_ctx = ubus_connect(NULL); |
| if (APP_ctx == NULL) |
| { |
| printf(" %s Failed to connect to ubus!\n", __FUNCTION__); |
| return -1; |
| } |
| |
| ubus_add_uloop(APP_ctx); |
| // lookup audio_if until success |
| while(1) |
| { |
| rc = ubus_lookup_id(APP_ctx, AUDIO_UBUS_ID, & APP_audio_request_id); |
| if (0 != rc) |
| { |
| printf("%s, Failed to look up(%s), rc=%d\n", __FUNCTION__, AUDIO_UBUS_ID, rc); |
| usleep(100000); //100ms |
| } |
| else |
| break; |
| } |
| printf("%s, ubus_lookup_id(%s) OK\n", __FUNCTION__, AUDIO_UBUS_ID); |
| return 0; |
| } |
| |
| static void APP_Audio_GaniSet_cb(struct ubus_request *req, int rc) |
| { |
| if (req) |
| { |
| free(req); |
| req = NULL; |
| } |
| printf("%s do nothing, rc=%d\n", __FUNCTION__, rc); |
| } |
| |
| |
| /**********************************************************************\ |
| * Function: APP_Audio_VolumeSet |
| * Description: This function is called to send command into audio_if. |
| * unsigned int volume(0~100) |
| * Returns: 0 on success |
| \**********************************************************************/ |
| static void APP_Audio_GainSet(unsigned char *gain, int gain_num) |
| { |
| int rc = 0; |
| struct ubus_request *req = NULL; |
| |
| req = (struct ubus_request *)malloc(sizeof(struct ubus_request)); |
| if (req == NULL) |
| { |
| printf("leave %s: lack of memory\n", __FUNCTION__); |
| return; |
| } |
| memset(req, 0, sizeof(struct ubus_request)); |
| blob_buf_init(&audio_cm_b, 0); |
| |
| char name[20]; |
| int i = 0; |
| for(; i < gain_num; i++) { |
| memset(name, 0x0, 20); |
| sprintf(name, "volume_gain_%d", i); |
| blobmsg_add_u8(&audio_cm_b, name, gain[i]); |
| } |
| |
| #if 1 |
| if ((rc = ubus_invoke_async(APP_ctx, APP_audio_request_id, AUDIO_UBUS_AUDIO_GAIN_SET, audio_cm_b.head, req)) != UBUS_STATUS_OK) |
| { |
| free(req); |
| printf("%s, ubus_invoke_async volume set failed: %s\n", __FUNCTION__, ubus_strerror(rc)); |
| } |
| else |
| { |
| printf("%s: ubus_invoke_async success\n", __FUNCTION__); |
| req->complete_cb = APP_Audio_GaniSet_cb; |
| ubus_complete_request_async(APP_ctx, req); |
| } |
| #else |
| if ((rc = ubus_invoke(APP_ctx, APP_audio_request_id, AUDIO_UBUS_AUDIO_GAIN_SET, audio_cm_b.head, NULL, 0, 0)) != UBUS_STATUS_OK) |
| { |
| printf("%s, ubus_invoke_async volume set failed: %s\n", __FUNCTION__, ubus_strerror(rc)); |
| } |
| else |
| { |
| printf("%s: ubus_invoke_async success\n", __FUNCTION__); |
| } |
| free(req); |
| |
| #endif |
| } |
| |
| /**********************************************************************\ |
| * Function: main |
| * Description: Main function of this APP. |
| * Returns: 0 on success |
| \**********************************************************************/ |
| int main (int argc ,char *argv[]) |
| { |
| pthread_attr_t tattr; |
| |
| /*Init ubus server*/ |
| if(APP_uBusInit()) |
| return -1; |
| |
| #if 0 |
| /*Create thread to accept user choice*/ |
| pthread_attr_init(&tattr); |
| pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED); |
| pthread_create(&APP_MainLoopTask, &tattr, (void *)APP_MainLoop, NULL); |
| #else |
| unsigned char gain[] = {0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0}; |
| APP_Audio_GainSet(gain, sizeof(gain)); |
| |
| #endif |
| |
| uloop_run(); |
| |
| printf("Here, uloop stopped!!!\n "); |
| |
| /*unregister uloop*/ |
| /* thread will get here only if uloop stopped*/ |
| ubus_free(APP_ctx); |
| uloop_done(); |
| return 0; |
| } |
| |