1/*
2 * Copyright 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/******************************************************************************
18 *
19 *  Filename:      hardware.c
20 *
21 *  Description:   Contains controller-specific functions, like
22 *                      firmware patch download
23 *                      low power mode operations
24 *
25 ******************************************************************************/
26
27#define LOG_TAG "bt_vendor"
28
29#include <utils/Log.h>
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <signal.h>
33#include <time.h>
34#include <errno.h>
35#include <fcntl.h>
36#include <dirent.h>
37#include <ctype.h>
38#include <cutils/properties.h>
39#include <stdlib.h>
40#include "bt_hci_bdroid.h"
41#include "bt_vendor_qcom.h"
42#include <string.h>
43#define MAX_CNT_RETRY 100
44
45int hw_config(int nState)
46{
47    char *szState[] = {"true", "false"};
48    char *szReqSt = NULL;
49    char szBtSocStatus[PROPERTY_VALUE_MAX] = {'\0', };
50
51    if(nState == BT_VND_PWR_OFF)
52        szReqSt = szState[1];
53    else
54        szReqSt = szState[0];
55
56    if((property_get("bluetooth.status", szBtSocStatus, "") <= 0))
57    {
58       if(nState == BT_VND_PWR_ON ) {
59          ALOGW("Hw_config: First Time BT on after boot.Starting hciattach daemon BTStatus=%s",szBtSocStatus);
60          if (property_set("bluetooth.hciattach", szReqSt) < 0)
61          {
62              ALOGE("Hw_config: Property Setting fail");
63              return -1;
64          }
65       }
66    } else if( !(strncmp(szBtSocStatus, "on", strlen("on")))) {
67          //BTSOC is already on
68          ALOGW("Hw_config: nState = %d", nState);
69    } else {
70          ALOGW("Hw_config: trigerring hciattach");
71          if (property_set("bluetooth.hciattach", szReqSt) < 0)
72          {
73              ALOGE("Hw_config: Property Setting fail");
74              return -1;
75          }
76    }
77
78    return 0;
79}
80
81int readTrpState()
82{
83    char szBtStatus[PROPERTY_VALUE_MAX] = {0, };
84    if(property_get("bluetooth.status", szBtStatus, "") < 0){
85        ALOGE("Fail to get bluetooth status");
86        return FALSE;
87    }
88
89    if(!strncmp(szBtStatus, "on", strlen("on"))){
90        ALOGI("bluetooth status is on");
91        return TRUE;
92    }
93    return FALSE;
94}
95
96int is_hw_ready()
97{
98    int i=0;
99    char szStatus[10] = {0,};
100
101    for(i=MAX_CNT_RETRY; i>0; i--){
102       //TODO :: checking routine
103       if(readTrpState()==TRUE){
104           break;
105       }
106       usleep(50*1000);
107    }
108    return (i==0)? FALSE:TRUE;
109}
110
111#if (HW_NEED_END_WITH_HCI_RESET == TRUE)
112/*******************************************************************************
113**
114** Function         hw_epilog_cback
115**
116** Description      Callback function for Command Complete Events from HCI
117**                  commands sent in epilog process.
118**
119** Returns          None
120**
121*******************************************************************************/
122void hw_epilog_cback(void *p_mem)
123{
124    HC_BT_HDR *p_evt_buf = (HC_BT_HDR *) p_mem;
125    char        *p_name, *p_tmp;
126    uint8_t     *p, status;
127    uint16_t    opcode;
128
129    status = *((uint8_t *)(p_evt_buf + 1) + HCI_EVT_CMD_CMPL_STATUS_RET_BYTE);
130    p = (uint8_t *)(p_evt_buf + 1) + HCI_EVT_CMD_CMPL_OPCODE;
131    STREAM_TO_UINT16(opcode,p);
132
133    ALOGI("%s Opcode:0x%04X Status: %d", __FUNCTION__, opcode, status);
134
135    if (bt_vendor_cbacks)
136    {
137        /* Must free the RX event buffer */
138        bt_vendor_cbacks->dealloc(p_evt_buf);
139
140        /* Once epilog process is done, must call callback to notify caller */
141        bt_vendor_cbacks->epilog_cb(BT_VND_OP_RESULT_SUCCESS);
142    }
143}
144
145/*******************************************************************************
146**
147** Function         hw_epilog_process
148**
149** Description      Sample implementation of epilog process
150**
151** Returns          None
152**
153*******************************************************************************/
154void hw_epilog_process(void)
155{
156    HC_BT_HDR  *p_buf = NULL;
157    uint8_t     *p;
158
159    ALOGI("hw_epilog_process");
160
161    /* Sending a HCI_RESET */
162    if (bt_vendor_cbacks)
163    {
164        /* Must allocate command buffer via HC's alloc API */
165        p_buf = (HC_BT_HDR *) bt_vendor_cbacks->alloc(BT_HC_HDR_SIZE + \
166                                                       HCI_CMD_PREAMBLE_SIZE);
167    }
168
169    if (p_buf)
170    {
171        p_buf->event = MSG_STACK_TO_HC_HCI_CMD;
172        p_buf->offset = 0;
173        p_buf->layer_specific = 0;
174        p_buf->len = HCI_CMD_PREAMBLE_SIZE;
175
176        p = (uint8_t *) (p_buf + 1);
177        UINT16_TO_STREAM(p, HCI_RESET);
178        *p = 0; /* parameter length */
179
180        /* Send command via HC's xmit_cb API */
181        bt_vendor_cbacks->xmit_cb(HCI_RESET, p_buf, hw_epilog_cback);
182    }
183    else
184    {
185        if (bt_vendor_cbacks)
186        {
187            ALOGE("vendor lib epilog process aborted [no buffer]");
188            bt_vendor_cbacks->epilog_cb(BT_VND_OP_RESULT_FAIL);
189        }
190    }
191}
192#endif // (HW_NEED_END_WITH_HCI_RESET == TRUE)
193