daemon: Murphy/resource interfacing code overhaul.
[profile/ivi/speech-recognition.git] / src / daemon / client.h
1 /*
2  * Copyright (c) 2012, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *   * Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *   * Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *   * Neither the name of Intel Corporation nor the names of its contributors
14  *     may be used to endorse or promote products derived from this software
15  *     without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #ifndef __SRS_DAEMON_CLIENT_H__
31 #define __SRS_DAEMON_CLIENT_H__
32
33 typedef struct srs_client_s srs_client_t;
34
35 #include "src/daemon/context.h"
36 #include "src/daemon/resctl.h"
37 #include "src/daemon/audiobuf.h"
38 #include "srs/daemon/voice.h"
39
40
41 /*
42  * client types
43  */
44
45 typedef enum {
46     SRS_CLIENT_TYPE_NONE = 0,
47     SRS_CLIENT_TYPE_DBUS,                /* external D-BUS client */
48     SRS_CLIENT_TYPE_BUILTIN,             /* builtin client */
49 } srs_client_type_t;
50
51
52 /*
53  * voice focus types
54  */
55
56 typedef enum {
57     SRS_VOICE_FOCUS_NONE = 0,            /* focus released */
58     SRS_VOICE_FOCUS_SHARED,              /* normal shared voice focus */
59     SRS_VOICE_FOCUS_EXCLUSIVE,           /* exclusive voice focus */
60 } srs_voice_focus_t;
61
62
63 /*
64  * client commands
65  */
66
67 #define SRS_MAX_TOKENS 64
68
69 typedef struct {
70     char **tokens;                       /* tokens of this command */
71     int    ntoken;                       /* number of tokens */
72 } srs_command_t;
73
74
75 /*
76  * special command tokens
77  */
78
79 #define SRS_TOKEN_SWITCHDICT "__switch_dict__"
80 #define SRS_TOKEN_PUSHDICT   "__push_dict__"
81 #define SRS_TOKEN_POPDICT    "__pop_dict__"
82 #define SRS_TOKEN_WILDCARD   "*"
83
84
85 /* dictionary pseudo-commands */
86 #define SRS_DICTCMD_SWITCH    "__switch_dict__"
87 #define SRS_DICTCMD_PUSH      "__push_dict__"
88 #define SRS_DICTCMD_POP       "__pop_dict__"
89
90 #define SRS_DICT_SWITCH(dict) SRS_DICTCMD_SWITCH"("dict")"
91 #define SRS_DICT_PUSH(dict)   SRS_DICTCMD_PUSH"("dict")"
92 #define SRS_DICT_POP()        SRS_DICTCMD_POP
93
94
95 /*
96  * special tokens
97  */
98
99 #define SRS_TOKEN_SWITCHDICT "__switch_dict__"
100 #define SRS_TOKEN_PUSHDICT   "__push_dict__"
101 #define SRS_TOKEN_POPDICT    "__pop_dict__"
102
103 #define SRS_TOKEN_WILDCARD "*"           /* match till end of utterance */
104
105 /*
106  * dictionary operations
107  */
108
109 /* dictionary operations */
110 typedef enum {
111     SRS_DICT_OP_UNKNOWN = 0,
112     SRS_DICT_OP_SWITCH,
113     SRS_DICT_OP_PUSH,
114     SRS_DICT_OP_POP
115 } srs_dict_op_t;
116
117
118 /*
119  * connected clients
120  */
121
122 typedef struct {
123     /* recognizer interface */
124     int (*notify_focus)(srs_client_t *c, srs_voice_focus_t focus);
125     int (*notify_command)(srs_client_t *c, int idx, int ntoken,
126                           char **tokens, uint32_t *start, uint32_t *end,
127                           srs_audiobuf_t *audio);
128     /* voice rendering interface */
129     int (*notify_render)(srs_client_t *c, srs_voice_event_t *event);
130 } srs_client_ops_t;
131
132
133 struct srs_client_s {
134     mrp_list_hook_t         hook;        /* to list of clients */
135     srs_client_type_t       type;        /* client type */
136     char                   *name;        /* client name */
137     char                   *appclass;    /* client application class */
138     srs_command_t          *commands;    /* client command set */
139     int                     ncommand;    /* number of commands */
140     char                   *id;          /* client id */
141     srs_context_t          *srs;         /* context back pointer */
142     srs_resset_t           *rset;        /* resource set */
143     srs_voice_focus_t       requested;   /* requested voice focus */
144     int                     granted;     /* granted resources */
145     int                     enabled : 1; /* interested in commands */
146     int                     shared : 1;  /* whether voice focus is shared */
147     mrp_list_hook_t         voices;      /* unfinished voice requests */
148     srs_client_ops_t        ops;         /* client ops (notifications)  */
149     void                   *user_data;   /* opaque client data */
150 };
151
152
153 /** Create a new client. */
154 srs_client_t *client_create(srs_context_t *srs, srs_client_type_t type,
155                             const char *name, const char *appclass,
156                             char **commands, int ncommand,
157                             const char *id, srs_client_ops_t *ops,
158                             void *user_data);
159
160 /** Destroy a client. */
161 void client_destroy(srs_client_t *c);
162
163 /** Look up a client by its id. */
164 srs_client_t *client_lookup_by_id(srs_context_t *srs, const char *id);
165
166 /** Request client focus change. */
167 int client_request_focus(srs_client_t *c, srs_voice_focus_t focus);
168
169 /** Deliver a command notification event to the client. */
170 void client_notify_command(srs_client_t *c, int idx, int ntoken,
171                            const char **tokens, uint32_t *start, uint32_t *end,
172                            srs_audiobuf_t *audio);
173
174 /** Request synthesizing a message. */
175 uint32_t client_render_voice(srs_client_t *c, const char *msg,
176                              const char *voice, int timeout, int notify_events);
177
178 /** Cancel/stop a synthesizing request. */
179 void client_cancel_voice(srs_client_t *c, uint32_t id);
180
181 /** Create resources for all registered clients. */
182 void client_create_resources(srs_context_t *srs);
183
184 /** Reset the resource sets of all clients. */
185 void client_reset_resources(srs_context_t *srs);
186
187 /** Query voice actors. */
188 int client_query_voices(srs_client_t *c, const char *language,
189                         srs_voice_actor_t **actorsp);
190
191 /** Free voice actor query reult. */
192     void client_free_queried_voices(srs_voice_actor_t *actors);
193
194 #endif /* __SRS_DAEMON_CLIENT_H__ */