1 /* 2 * Copyright (C) 2021-2023 Alibaba Group Holding Limited 3 */ 4 5 #include <stdio.h> 6 #include <string.h> 7 #include "ulog/ulog.h" 8 #include "aos/kernel.h" 9 #include "aiconfig.h" 10 #include "aiagent_service.h" 11 #ifdef CONFIG_UCLOUD_AI_ENGINE_ENABLE 12 #include "engine/ucloud_ai_engine.h" 13 #endif 14 #ifdef CONFIG_KWS_AI_ENGINE_ENABLE 15 #include "engine/kws_engine.h" 16 #endif 17 18 #define TAG "AIAGENT_ENGINE" 19 20 static aiagent_engine_t *g_ai_engine = NULL; 21 22 #ifdef CONFIG_UCLOUD_AI_ENGINE_ENABLE 23 extern aiagent_context_t ucloud_ai_engine; 24 #endif 25 26 #ifdef CONFIG_KWS_AI_ENGINE_ENABLE 27 extern aiagent_context_t kws_engine; 28 #endif 29 30 /* Available ai engine context */ 31 static aiagent_context_t *ai_engine_ctx[] = { 32 #ifdef CONFIG_UCLOUD_AI_ENGINE_ENABLE 33 &ucloud_ai_engine, 34 #endif 35 #ifdef CONFIG_KWS_AI_ENGINE_ENABLE 36 &kws_engine, 37 #endif 38 NULL 39 }; 40 41 /* 42 * Initialize the ai engine 43 */ aiagent_engine_init(const char * dev_name)44aiagent_engine_t *aiagent_engine_init(const char *dev_name) 45 { 46 aiagent_engine_t *engine; 47 int32_t index; 48 int32_t i; 49 50 /*Reset ai engine service*/ 51 if (g_ai_engine != NULL) 52 aiagent_engine_uninit(); 53 54 index = 0; 55 engine = NULL; 56 if (dev_name == NULL) 57 return NULL; 58 59 if (dev_name != NULL) { 60 for (i = 0; ai_engine_ctx[i]; ++i) { 61 if (strncasecmp(ai_engine_ctx[i]->name, dev_name, strlen(dev_name)) == 0) { 62 if (ai_engine_ctx[i]->available()) { 63 engine = ai_engine_ctx[i]->create(index); 64 break; 65 } 66 } 67 } 68 } else { 69 for (i = 0; ai_engine_ctx[i]; ++i) { 70 if (ai_engine_ctx[i]->available()) { 71 engine = ai_engine_ctx[i]->create(index); 72 if (engine != NULL) 73 break; 74 75 } 76 } 77 } 78 if (engine == NULL) { 79 if (dev_name) { 80 LOGE(TAG, "%s not available", dev_name); 81 return NULL; 82 } 83 LOGE(TAG, "No available engine engine"); 84 return NULL; 85 } 86 g_ai_engine = engine; 87 g_ai_engine->name = ai_engine_ctx[i]->name; 88 89 /* Initialize the ai engine */ 90 if (g_ai_engine->ai_engine_init(g_ai_engine) < 0) { 91 aiagent_engine_uninit(); 92 return NULL; 93 } 94 95 LOG("Initialize ai agent engine successfully\n"); 96 return g_ai_engine; 97 } 98 99 /* 100 * Uninitialize the ai engine 101 */ aiagent_engine_uninit(void)102void aiagent_engine_uninit(void) 103 { 104 int32_t i, j; 105 106 if (!g_ai_engine) 107 return; 108 109 g_ai_engine->ai_engine_uninit(g_ai_engine); 110 g_ai_engine->ai_engine_free(g_ai_engine); 111 g_ai_engine = NULL; 112 113 LOG("Uninitialize ai agent engine successfully\n"); 114 return; 115 } 116 aiagent_engine_config(ai_config_t * config)117void aiagent_engine_config(ai_config_t *config) 118 { 119 g_ai_engine->config = config; 120 g_ai_engine->ai_engine_config(g_ai_engine); 121 } 122 aiagent_get_engine_name(void)123const char *aiagent_get_engine_name(void) 124 { 125 if (!g_ai_engine) 126 return NULL; 127 128 return g_ai_engine->name; 129 } 130 aiagent_get_engine(void)131aiagent_engine_t *aiagent_get_engine(void) 132 { 133 return g_ai_engine; 134 } 135