1/******************************************************************************
2 *
3 *  Copyright (C) 1999-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#include "OverrideLog.h"
19#define LOG_TAG "NfcNciHal"
20#include "gki.h"
21extern "C"
22{
23    #include "nfc_hal_nv_co.h"
24}
25#include "nfc_hal_nv_ci.h"
26#include "nfc_hal_int.h"
27#include "config.h"
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <fcntl.h>
31#include <errno.h>
32#include <string>
33
34
35//directory of HAL's non-volatile storage
36static const char* default_location = "/data/nfc";
37static const char* filename_prefix = "/halStorage.bin";
38static const std::string get_storage_location ();
39
40
41/*******************************************************************************
42**
43** Function         nfc_hal_nv_co_read
44**
45** Description      This function is called by NFA to read in data from the
46**                  previously opened file.
47**
48** Parameters       p_buf   - buffer to read the data into.
49**                  nbytes  - number of bytes to read into the buffer.
50**
51** Returns          void
52**
53**                  Note: Upon completion of the request, nfc_hal_nv_ci_read () is
54**                        called with the buffer of data, along with the number
55**                        of bytes read into the buffer, and a status.  The
56**                        call-in function should only be called when ALL requested
57**                        bytes have been read, the end of file has been detected,
58**                        or an error has occurred.
59**
60*******************************************************************************/
61void nfc_hal_nv_co_read (UINT8 *p_buf, UINT16 nbytes, UINT8 block)
62{
63    std::string fn = get_storage_location();
64    char filename[256];
65
66    fn.append (filename_prefix);
67    if (fn.length() > 200)
68    {
69        ALOGE ("%s: filename too long", __FUNCTION__);
70        return;
71    }
72    sprintf (filename, "%s%u", fn.c_str(), block);
73
74    ALOGD ("%s: buffer len=%u; file=%s", __FUNCTION__, nbytes, filename);
75    int fileStream = open (filename, O_RDONLY);
76    if (fileStream >= 0)
77    {
78        size_t actualRead = read (fileStream, p_buf, nbytes);
79        if (actualRead > 0)
80        {
81            ALOGD ("%s: read bytes=%u", __FUNCTION__, actualRead);
82            nfc_hal_nv_ci_read (actualRead, NFC_HAL_NV_CO_OK, block);
83        }
84        else
85        {
86            ALOGE ("%s: fail to read", __FUNCTION__);
87            nfc_hal_nv_ci_read (actualRead, NFC_HAL_NV_CO_FAIL, block);
88        }
89        close (fileStream);
90    }
91    else
92    {
93        ALOGD ("%s: fail to open", __FUNCTION__);
94        nfc_hal_nv_ci_read (0, NFC_HAL_NV_CO_FAIL, block);
95    }
96}
97
98
99/*******************************************************************************
100**
101** Function         nfc_hal_nv_co_write
102**
103** Description      This function is called by io to send file data to the
104**                  phone.
105**
106** Parameters       p_buf   - buffer to read the data from.
107**                  nbytes  - number of bytes to write out to the file.
108**
109** Returns          void
110**
111**                  Note: Upon completion of the request, nfc_hal_nv_ci_write () is
112**                        called with the file descriptor and the status.  The
113**                        call-in function should only be called when ALL requested
114**                        bytes have been written, or an error has been detected,
115**
116*******************************************************************************/
117void nfc_hal_nv_co_write (const UINT8 *p_buf, UINT16 nbytes, UINT8 block)
118{
119    std::string fn = get_storage_location();
120    char filename[256];
121    int fileStream = 0;
122
123    fn.append (filename_prefix);
124    if (fn.length() > 200)
125    {
126        ALOGE ("%s: filename too long", __FUNCTION__);
127        return;
128    }
129    sprintf (filename, "%s%u", fn.c_str(), block);
130    ALOGD ("%s: bytes=%u; file=%s", __FUNCTION__, nbytes, filename);
131
132    fileStream = open (filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
133    if (fileStream >= 0)
134    {
135        size_t actualWritten = write (fileStream, p_buf, nbytes);
136        ALOGD ("%s: %d bytes written", __FUNCTION__, actualWritten);
137        if (actualWritten > 0) {
138            nfc_hal_nv_ci_write (NFC_HAL_NV_CO_OK);
139        }
140        else
141        {
142            ALOGE ("%s: fail to write", __FUNCTION__);
143            nfc_hal_nv_ci_write (NFC_HAL_NV_CO_FAIL);
144        }
145        close (fileStream);
146    }
147    else
148    {
149        ALOGE ("%s: fail to open, error = %d", __FUNCTION__, errno);
150        nfc_hal_nv_ci_write (NFC_HAL_NV_CO_FAIL);
151    }
152}
153
154
155/*******************************************************************************
156**
157** Function         get_storage_location
158**
159** Description      Get the absolute directory path of the HAL's storage location.
160**
161** Parameters       none
162**
163** Returns          Absolute path.
164**
165*******************************************************************************/
166const std::string get_storage_location ()
167{
168    char buffer [100];
169    memset (buffer, 0, sizeof(buffer));
170    if (!GetStrValue (NAME_NFA_STORAGE, buffer, sizeof(buffer)))
171        return default_location;
172    else
173        return std::string (buffer);
174}
175
176
177/*******************************************************************************
178**
179** Function         delete_hal_non_volatile_store
180**
181** Description      Delete all the content of the HAL's storage location.
182**
183** Parameters       none
184**
185** Returns          none
186**
187*******************************************************************************/
188void delete_hal_non_volatile_store ()
189{
190    static bool firstTime = true;
191    std::string fn = get_storage_location();
192    char filename[256];
193    int stat = 0;
194
195    if (firstTime == false)
196        return;
197    firstTime = false;
198
199    ALOGD ("%s", __FUNCTION__);
200
201    fn.append (filename_prefix);
202    if (fn.length() > 200)
203    {
204        ALOGE ("%s: filename too long", __FUNCTION__);
205        return;
206    }
207
208    sprintf (filename, "%s%u", fn.c_str(), DH_NV_BLOCK);
209    remove (filename);
210    sprintf (filename, "%s%u", fn.c_str(), HC_F3_NV_BLOCK);
211    remove (filename);
212    sprintf (filename, "%s%u", fn.c_str(), HC_F4_NV_BLOCK);
213    remove (filename);
214    sprintf (filename, "%s%u", fn.c_str(), HC_F2_NV_BLOCK);
215    remove (filename);
216}
217