The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame^] | 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 | #include "gki.h" |
| 20 | extern "C" |
| 21 | { |
| 22 | #include "nfc_hal_nv_co.h" |
| 23 | } |
| 24 | #include "nfc_hal_nv_ci.h" |
| 25 | #include "nfc_hal_int.h" |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <errno.h> |
| 30 | #include <string> |
| 31 | |
| 32 | |
| 33 | //directory of HAL's non-volatile storage |
| 34 | static const char* bcm_nfc_location = "/data/nfc"; |
| 35 | static const char* filename_prefix = "/halStorage.bin"; |
| 36 | |
| 37 | |
| 38 | /******************************************************************************* |
| 39 | ** |
| 40 | ** Function nfc_hal_nv_co_read |
| 41 | ** |
| 42 | ** Description This function is called by NFA to read in data from the |
| 43 | ** previously opened file. |
| 44 | ** |
| 45 | ** Parameters p_buf - buffer to read the data into. |
| 46 | ** nbytes - number of bytes to read into the buffer. |
| 47 | ** |
| 48 | ** Returns void |
| 49 | ** |
| 50 | ** Note: Upon completion of the request, nfc_hal_nv_ci_read () is |
| 51 | ** called with the buffer of data, along with the number |
| 52 | ** of bytes read into the buffer, and a status. The |
| 53 | ** call-in function should only be called when ALL requested |
| 54 | ** bytes have been read, the end of file has been detected, |
| 55 | ** or an error has occurred. |
| 56 | ** |
| 57 | *******************************************************************************/ |
| 58 | void nfc_hal_nv_co_read (UINT8 *p_buf, UINT16 nbytes, UINT8 block) |
| 59 | { |
| 60 | std::string fn (bcm_nfc_location); |
| 61 | char filename[256]; |
| 62 | |
| 63 | fn.append (filename_prefix); |
| 64 | if (fn.length() > 200) |
| 65 | { |
| 66 | ALOGE ("%s: filename too long", __FUNCTION__); |
| 67 | return; |
| 68 | } |
| 69 | sprintf (filename, "%s%u", fn.c_str(), block); |
| 70 | |
| 71 | ALOGD ("%s: buffer len=%u; file=%s", __FUNCTION__, nbytes, filename); |
| 72 | int fileStream = open (filename, O_RDONLY); |
| 73 | if (fileStream > 0) |
| 74 | { |
| 75 | size_t actualRead = read (fileStream, p_buf, nbytes); |
| 76 | if (actualRead > 0) |
| 77 | { |
| 78 | ALOGD ("%s: read bytes=%u", __FUNCTION__, actualRead); |
| 79 | nfc_hal_nv_ci_read (actualRead, NFC_HAL_NV_CO_OK, block); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | ALOGE ("%s: fail to read", __FUNCTION__); |
| 84 | nfc_hal_nv_ci_read (actualRead, NFC_HAL_NV_CO_FAIL, block); |
| 85 | } |
| 86 | close (fileStream); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | ALOGD ("%s: fail to open", __FUNCTION__); |
| 91 | nfc_hal_nv_ci_read (0, NFC_HAL_NV_CO_FAIL, block); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /******************************************************************************* |
| 97 | ** |
| 98 | ** Function nfc_hal_nv_co_write |
| 99 | ** |
| 100 | ** Description This function is called by io to send file data to the |
| 101 | ** phone. |
| 102 | ** |
| 103 | ** Parameters p_buf - buffer to read the data from. |
| 104 | ** nbytes - number of bytes to write out to the file. |
| 105 | ** |
| 106 | ** Returns void |
| 107 | ** |
| 108 | ** Note: Upon completion of the request, nfc_hal_nv_ci_write () is |
| 109 | ** called with the file descriptor and the status. The |
| 110 | ** call-in function should only be called when ALL requested |
| 111 | ** bytes have been written, or an error has been detected, |
| 112 | ** |
| 113 | *******************************************************************************/ |
| 114 | void nfc_hal_nv_co_write (const UINT8 *p_buf, UINT16 nbytes, UINT8 block) |
| 115 | { |
| 116 | std::string fn (bcm_nfc_location); |
| 117 | char filename[256]; |
| 118 | int fileStream = 0; |
| 119 | |
| 120 | fn.append (filename_prefix); |
| 121 | if (fn.length() > 200) |
| 122 | { |
| 123 | ALOGE ("%s: filename too long", __FUNCTION__); |
| 124 | return; |
| 125 | } |
| 126 | sprintf (filename, "%s%u", fn.c_str(), block); |
| 127 | ALOGD ("%s: bytes=%u; file=%s", __FUNCTION__, nbytes, filename); |
| 128 | |
| 129 | fileStream = open (filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); |
| 130 | if (fileStream > 0) |
| 131 | { |
| 132 | size_t actualWritten = write (fileStream, p_buf, nbytes); |
| 133 | ALOGD ("%s: %d bytes written", __FUNCTION__, actualWritten); |
| 134 | if (actualWritten > 0) { |
| 135 | nfc_hal_nv_ci_write (NFC_HAL_NV_CO_OK); |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | ALOGE ("%s: fail to write", __FUNCTION__); |
| 140 | nfc_hal_nv_ci_write (NFC_HAL_NV_CO_FAIL); |
| 141 | } |
| 142 | close (fileStream); |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | ALOGE ("%s: fail to open, error = %d", __FUNCTION__, errno); |
| 147 | nfc_hal_nv_ci_write (NFC_HAL_NV_CO_FAIL); |
| 148 | } |
| 149 | } |