1/******************************************************************************
2 *
3 *  Copyright (C) 2003-2012 Broadcom Corporation
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at:
8 *
9 *  http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *
17 ******************************************************************************/
18
19/******************************************************************************
20 *
21 *  This is the implementation file for the file system call-in functions.
22 *
23 ******************************************************************************/
24#include <string.h>
25
26#include "bta_api.h"
27#include "bta_sys.h"
28#include "bta_fs_ci.h"
29#include "gki.h"
30#include "bd.h"
31
32/*******************************************************************************
33**
34** Function         bta_fs_ci_write
35**
36** Description      This function sends an event to IO indicating the phone
37**                  has written the number of bytes specified in the call-out
38**                  function, bta_fs_co_write(), and is ready for more data.
39**                  This function is used to control the TX data flow.
40**                  Note: The data buffer is released by the stack after
41**                        calling this function.
42**
43** Parameters       fd - file descriptor passed to the stack in the
44**                       bta_fs_ci_open call-in function.
45**                  status - BTA_FS_CO_OK or BTA_FS_CO_FAIL
46**
47** Returns          void
48**
49*******************************************************************************/
50void bta_fs_ci_write(int fd, tBTA_FS_CO_STATUS status, UINT16 evt)
51{
52    tBTA_FS_CI_WRITE_EVT  *p_evt;
53
54    if ((p_evt = (tBTA_FS_CI_WRITE_EVT *) GKI_getbuf(sizeof(tBTA_FS_CI_WRITE_EVT))) != NULL)
55    {
56        p_evt->hdr.event = evt;
57        p_evt->fd = fd;
58        p_evt->status = status;
59
60        bta_sys_sendmsg(p_evt);
61    }
62}
63
64/*******************************************************************************
65**
66** Function         bta_fs_ci_read
67**
68** Description      This function sends an event to BTA indicating the phone has
69**                  read in the requested amount of data specified in the
70**                  bta_fs_co_read() call-out function.  It should only be called
71**                  when the requested number of bytes has been read in, or after
72**                  the end of the file has been detected.
73**
74** Parameters       fd - file descriptor passed to the stack in the
75**                       bta_fs_ci_open call-in function.
76**                  num_bytes_read - number of bytes read into the buffer
77**                      specified in the read callout-function.
78**                  status - BTA_FS_CO_OK if full buffer of data,
79**                           BTA_FS_CO_EOF if the end of file has been reached,
80**                           BTA_FS_CO_FAIL if an error has occurred.
81**
82** Returns          void
83**
84*******************************************************************************/
85void bta_fs_ci_read(int fd, UINT16 num_bytes_read, tBTA_FS_CO_STATUS status, UINT16 evt)
86{
87    tBTA_FS_CI_READ_EVT  *p_evt;
88
89    if ((p_evt = (tBTA_FS_CI_READ_EVT *) GKI_getbuf(sizeof(tBTA_FS_CI_READ_EVT))) != NULL)
90    {
91        p_evt->hdr.event = evt;
92        p_evt->fd        = fd;
93        p_evt->status    = status;
94        p_evt->num_read  = num_bytes_read;
95
96        bta_sys_sendmsg(p_evt);
97    }
98}
99
100/*******************************************************************************
101**
102** Function         bta_fs_ci_open
103**
104** Description      This function sends an event to BTA indicating the phone has
105**                  finished opening a file for reading or writing.
106**
107** Parameters       fd - file descriptor passed to the stack in the
108**                       bta_fs_ci_open call-in function.
109**                  status - BTA_FS_CO_OK if file was opened in mode specified
110**                                          in the call-out function.
111**                           BTA_FS_CO_EACCES if the file exists, but contains
112**                                          the wrong access permissions.
113**                           BTA_FS_CO_FAIL if any other error has occurred.
114**                  file_size - The total size of the file
115**                  evt - Used Internally by BTA -> MUST be same value passed
116**                       in call-out function.
117**
118** Returns          void
119**
120*******************************************************************************/
121void bta_fs_ci_open(int fd, tBTA_FS_CO_STATUS status, UINT32 file_size, UINT16 evt)
122{
123    tBTA_FS_CI_OPEN_EVT  *p_evt;
124
125    if ((p_evt = (tBTA_FS_CI_OPEN_EVT *) GKI_getbuf(sizeof(tBTA_FS_CI_OPEN_EVT))) != NULL)
126    {
127        p_evt->hdr.event = evt;
128        p_evt->fd        = fd;
129        p_evt->status    = status;
130        p_evt->file_size = file_size;
131        p_evt->p_file    = NULL;
132
133        bta_sys_sendmsg(p_evt);
134    }
135}
136
137/*******************************************************************************
138**
139** Function         bta_fs_ci_direntry
140**
141** Description      This function is called in response to the
142**                  bta_fs_co_getdirentry call-out function.
143**
144** Parameters       status - BTA_FS_CO_OK if p_entry points to a valid entry.
145**                           BTA_FS_CO_EODIR if no more entries (p_entry is ignored).
146**                           BTA_FS_CO_FAIL if any errors have occurred.
147**
148** Returns          void
149**
150*******************************************************************************/
151void bta_fs_ci_direntry(tBTA_FS_CO_STATUS status, UINT16 evt)
152{
153    tBTA_FS_CI_GETDIR_EVT  *p_evt;
154
155    if ((p_evt = (tBTA_FS_CI_GETDIR_EVT *)GKI_getbuf(sizeof(tBTA_FS_CI_GETDIR_EVT))) != NULL)
156    {
157        p_evt->hdr.event = evt;
158        p_evt->status = status;
159        bta_sys_sendmsg(p_evt);
160    }
161}
162
163/*******************************************************************************
164**
165** Function         bta_fs_ci_resume
166**
167** Description      This function is called in response to the
168**                  bta_fs_co_resume call-out function.
169**
170** Parameters       p_sess_info - the stored session ID and related information.
171**                  timeout - the timeout for this suspended session.
172**                  ssn     - the stored session sequence number.
173**                  info    - the stored BTA specific information (like last active operation).
174**                  status  - BTA_FS_CO_OK if p_entry points to a valid entry.
175**                            BTA_FS_CO_FAIL if any errors have occurred.
176**                  evt - Used Internally by BTA -> MUST be same value passed
177**                       in call-out function.
178**
179** Returns          void
180**
181*******************************************************************************/
182void bta_fs_ci_resume (BD_ADDR_PTR p_addr, UINT8 *p_sess_info,
183                       UINT32 timeout, UINT32 offset, UINT8 ssn, UINT8 info,
184                       tBTA_FS_CO_STATUS status, UINT16 evt)
185{
186    tBTA_FS_CI_RESUME_EVT  *p_evt;
187    UINT16  size = sizeof(tBTA_FS_CI_RESUME_EVT) + sizeof(BD_ADDR);
188
189    if ((p_evt = (tBTA_FS_CI_RESUME_EVT *)GKI_getbuf(size)) != NULL)
190    {
191        p_evt->p_addr       = NULL;
192        if (p_addr != NULL)
193        {
194            p_evt->p_addr   = (BD_ADDR_PTR)(p_evt + 1);
195            bdcpy(p_evt->p_addr, p_addr);
196        }
197        p_evt->hdr.event    = evt;
198        p_evt->p_sess_info  = p_sess_info;
199        p_evt->timeout      = timeout;
200        p_evt->offset       = offset;
201        p_evt->ssn          = ssn;
202        p_evt->info         = info;
203        p_evt->status       = status;
204        bta_sys_sendmsg(p_evt);
205    }
206}
207
208/*******************************************************************************
209**
210** Function         bta_fs_ci_action
211**
212** Description      This function is called in response to one of the action
213**                  call-out functions: bta_fs_co_copy, bta_fs_co_rename or
214**                  bta_fs_co_set_perms.
215**
216** Parameters       status  - BTA_FS_CO_OK if the action is succession.
217**                            BTA_FS_CO_FAIL if any errors have occurred.
218**                  evt - Used Internally by BTA -> MUST be same value passed
219**                       in call-out function.
220**
221** Returns          void
222**
223*******************************************************************************/
224void bta_fs_ci_action(tBTA_FS_CO_STATUS status, UINT16 evt)
225{
226    tBTA_FS_CI_ACTION_EVT  *p_evt;
227
228    if ((p_evt = (tBTA_FS_CI_ACTION_EVT *) GKI_getbuf(sizeof(tBTA_FS_CI_ACTION_EVT))) != NULL)
229    {
230        p_evt->hdr.event = evt;
231        p_evt->status    = status;
232
233        bta_sys_sendmsg(p_evt);
234    }
235}
236
237/*******************************************************************************
238**
239** Function         bta_fs_ci_resume_op
240**
241** Description      This function sends an event to BTA indicating the phone has
242**                  finished opening a file for reading or writing on resume.
243**
244** Parameters       fd - file descriptor passed to the stack in the
245**                       bta_fs_ci_open call-in function.
246**                  status - BTA_FS_CO_OK if file was opened in mode specified
247**                                          in the call-out function.
248**                           BTA_FS_CO_EACCES if the file exists, but contains
249**                                          the wrong access permissions.
250**                           BTA_FS_CO_FAIL if any other error has occurred.
251**                  p_file - The file name associated with fd
252**                  file_size - The total size of the file
253**                  evt - Used Internally by BTA -> MUST be same value passed
254**                       in call-out function.
255**
256** Returns          void
257**
258*******************************************************************************/
259void bta_fs_ci_resume_op(int fd, tBTA_FS_CO_STATUS status, const char *p_file,
260                         UINT32 file_size, UINT16 evt)
261{
262    tBTA_FS_CI_OPEN_EVT  *p_evt;
263    UINT16  file_len = strlen(p_file) + 1;
264    UINT16  size = sizeof(tBTA_FS_CI_OPEN_EVT) + file_len;
265    char *p;
266
267    if ((p_evt = (tBTA_FS_CI_OPEN_EVT *) GKI_getbuf(size)) != NULL)
268    {
269        p_evt->hdr.event = evt;
270        p_evt->fd        = fd;
271        p_evt->status    = status;
272        p_evt->file_size = file_size;
273        p   = (char *)(p_evt + 1);
274        BCM_STRNCPY_S (p, file_len, p_file, file_len-1);
275        p[file_len] = '\0';
276        p_evt->p_file    = (const char *)p;
277
278        bta_sys_sendmsg(p_evt);
279    }
280}
281