summaryrefslogtreecommitdiffstats
path: root/bta/sys
diff options
context:
space:
mode:
Diffstat (limited to 'bta/sys')
-rw-r--r--bta/sys/bd.c112
-rw-r--r--bta/sys/bta_sys.h308
-rw-r--r--bta/sys/bta_sys_cfg.c55
-rw-r--r--bta/sys/bta_sys_ci.c77
-rw-r--r--bta/sys/bta_sys_conn.c577
-rw-r--r--bta/sys/bta_sys_int.h119
-rw-r--r--bta/sys/bta_sys_main.c722
-rw-r--r--bta/sys/ptim.c162
-rw-r--r--bta/sys/utl.c296
9 files changed, 2428 insertions, 0 deletions
diff --git a/bta/sys/bd.c b/bta/sys/bd.c
new file mode 100644
index 0000000..052f9b9
--- /dev/null
+++ b/bta/sys/bd.c
@@ -0,0 +1,112 @@
+/******************************************************************************
+ *
+ * Copyright (C) 2003-2012 Broadcom Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ * BD address services.
+ *
+ ******************************************************************************/
+
+#include "data_types.h"
+#include "bd.h"
+
+/*****************************************************************************
+** Constants
+*****************************************************************************/
+
+/* global constant for "any" bd addr */
+const BD_ADDR bd_addr_any = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
+const BD_ADDR bd_addr_null= {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+
+/*****************************************************************************
+** Functions
+*****************************************************************************/
+
+/*******************************************************************************
+**
+** Function bdcpy
+**
+** Description Copy bd addr b to a.
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bdcpy(BD_ADDR a, const BD_ADDR b)
+{
+ int i;
+
+ for (i = BD_ADDR_LEN; i != 0; i--)
+ {
+ *a++ = *b++;
+ }
+}
+
+/*******************************************************************************
+**
+** Function bdcmp
+**
+** Description Compare bd addr b to a.
+**
+**
+** Returns Zero if b==a, nonzero otherwise (like memcmp).
+**
+*******************************************************************************/
+int bdcmp(const BD_ADDR a, const BD_ADDR b)
+{
+ int i;
+
+ for (i = BD_ADDR_LEN; i != 0; i--)
+ {
+ if (*a++ != *b++)
+ {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+/*******************************************************************************
+**
+** Function bdcmpany
+**
+** Description Compare bd addr to "any" bd addr.
+**
+**
+** Returns Zero if a equals bd_addr_any.
+**
+*******************************************************************************/
+int bdcmpany(const BD_ADDR a)
+{
+ return bdcmp(a, bd_addr_any);
+}
+
+/*******************************************************************************
+**
+** Function bdsetany
+**
+** Description Set bd addr to "any" bd addr.
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bdsetany(BD_ADDR a)
+{
+ bdcpy(a, bd_addr_any);
+}
diff --git a/bta/sys/bta_sys.h b/bta/sys/bta_sys.h
new file mode 100644
index 0000000..5d724e5
--- /dev/null
+++ b/bta/sys/bta_sys.h
@@ -0,0 +1,308 @@
+/******************************************************************************
+ *
+ * Copyright (C) 2003-2012 Broadcom Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ * This is the public interface file for the BTA system manager.
+ *
+ ******************************************************************************/
+#ifndef BTA_SYS_H
+#define BTA_SYS_H
+
+#include "bt_target.h"
+#include "gki.h"
+
+/*****************************************************************************
+** Constants and data types
+*****************************************************************************/
+
+/* event handler function type */
+typedef BOOLEAN (tBTA_SYS_EVT_HDLR)(BT_HDR *p_msg);
+
+/* disable function type */
+typedef void (tBTA_SYS_DISABLE)(void);
+
+
+/* HW modules */
+enum
+{
+ BTA_SYS_HW_BLUETOOTH,
+ BTA_SYS_HW_FMRX,
+ BTA_SYS_HW_FMTX,
+ BTA_SYS_HW_GPS,
+ BTA_SYS_HW_SENSOR,
+ BTA_SYS_HW_NFC,
+ BTA_SYS_HW_RT,
+
+ BTA_SYS_MAX_HW_MODULES
+};
+
+typedef UINT16 tBTA_SYS_HW_MODULE;
+
+#ifndef BTA_DM_NUM_JV_ID
+#define BTA_DM_NUM_JV_ID 2
+#endif
+
+/* SW sub-systems */
+#define BTA_ID_SYS 0 /* system manager */
+/* BLUETOOTH PART - from 0 to BTA_ID_BLUETOOTH_MAX */
+#define BTA_ID_DM 1 /* device manager */
+#define BTA_ID_DM_SEARCH 2 /* device manager search */
+#define BTA_ID_DM_SEC 3 /* device manager security */
+#define BTA_ID_DG 4 /* data gateway */
+#define BTA_ID_AG 5 /* audio gateway */
+#define BTA_ID_OPC 6 /* object push client */
+#define BTA_ID_OPS 7 /* object push server */
+#define BTA_ID_FTS 8 /* file transfer server */
+#define BTA_ID_CT 9 /* cordless telephony terminal */
+#define BTA_ID_FTC 10 /* file transfer client */
+#define BTA_ID_SS 11 /* synchronization server */
+#define BTA_ID_PR 12 /* Printer client */
+#define BTA_ID_BIC 13 /* Basic Imaging Client */
+#define BTA_ID_PAN 14 /* Personal Area Networking */
+#define BTA_ID_BIS 15 /* Basic Imaging Server */
+#define BTA_ID_ACC 16 /* Advanced Camera Client */
+#define BTA_ID_SC 17 /* SIM Card Access server */
+#define BTA_ID_AV 18 /* Advanced audio/video */
+#define BTA_ID_AVK 19 /* Audio/video sink */
+#define BTA_ID_HD 20 /* HID Device */
+#define BTA_ID_CG 21 /* Cordless Gateway */
+#define BTA_ID_BP 22 /* Basic Printing Client */
+#define BTA_ID_HH 23 /* Human Interface Device Host */
+#define BTA_ID_PBS 24 /* Phone Book Access Server */
+#define BTA_ID_PBC 25 /* Phone Book Access Client */
+#define BTA_ID_JV 26 /* Java */
+#define BTA_ID_HS 27 /* Headset */
+#define BTA_ID_MSE 28 /* Message Server Equipment */
+#define BTA_ID_MCE 29 /* Message Client Equipment */
+#define BTA_ID_HL 30 /* Health Device Profile*/
+#define BTA_ID_GATTC 31 /* GATT Client */
+#define BTA_ID_GATTS 32 /* GATT Client */
+#define BTA_ID_BLUETOOTH_MAX 33 /* last BT profile */
+
+/* FM */
+#define BTA_ID_FM 34 /* FM */
+#define BTA_ID_FMTX 35 /* FM TX */
+
+/* SENSOR */
+#define BTA_ID_SSR 36 /* Sensor */
+
+/* GPS */
+#define BTA_ID_GPS 37 /* GPS */
+
+/* GENERIC */
+#define BTA_ID_PRM 38
+#define BTA_ID_SYSTEM 39 /* platform-specific */
+#define BTA_ID_SWRAP 40 /* Insight script wrapper */
+#define BTA_ID_MIP 41 /* Multicase Individual Polling */
+#define BTA_ID_RT 42 /* Audio Routing module: This module is always on. */
+
+
+/* JV */
+#define BTA_ID_JV1 43 /* JV1 */
+#define BTA_ID_JV2 44 /* JV2 */
+
+#define BTA_ID_MAX (43 + BTA_DM_NUM_JV_ID)
+
+typedef UINT8 tBTA_SYS_ID;
+
+
+#define BTA_SYS_CONN_OPEN 0x00
+#define BTA_SYS_CONN_CLOSE 0x01
+#define BTA_SYS_APP_OPEN 0x02
+#define BTA_SYS_APP_CLOSE 0x03
+#define BTA_SYS_SCO_OPEN 0x04
+#define BTA_SYS_SCO_CLOSE 0x05
+#define BTA_SYS_CONN_IDLE 0x06
+#define BTA_SYS_CONN_BUSY 0x07
+
+/* for link policy */
+#define BTA_SYS_PLCY_SET 0x10 /* set the link policy to the given addr */
+#define BTA_SYS_PLCY_CLR 0x11 /* clear the link policy to the given addr */
+#define BTA_SYS_PLCY_DEF_SET 0x12 /* set the default link policy */
+#define BTA_SYS_PLCY_DEF_CLR 0x13 /* clear the default link policy */
+#define BTA_SYS_ROLE_CHANGE 0x14 /* role change */
+
+typedef UINT8 tBTA_SYS_CONN_STATUS;
+
+/* Bitmask of sys features */
+#define BTA_SYS_FEAT_PCM2 0x0001
+#define BTA_SYS_FEAT_PCM2_MASTER 0x0002
+
+/* tBTA_PREF_ROLES */
+typedef UINT8 tBTA_SYS_PREF_ROLES;
+
+/* conn callback for role / low power manager*/
+typedef void (tBTA_SYS_CONN_CBACK)(tBTA_SYS_CONN_STATUS status,UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
+
+/* conn callback for role / low power manager*/
+typedef void (tBTA_SYS_SSR_CFG_CBACK)(UINT8 id, UINT8 app_id, UINT16 latency, UINT16 tout);
+
+#if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&(BTA_EIR_CANNED_UUID_LIST != TRUE)
+/* eir callback for adding/removeing UUID */
+typedef void (tBTA_SYS_EIR_CBACK)(UINT16 uuid16, BOOLEAN adding);
+#endif
+
+/* registration structure */
+typedef struct
+{
+ tBTA_SYS_EVT_HDLR *evt_hdlr;
+ tBTA_SYS_DISABLE *disable;
+} tBTA_SYS_REG;
+
+/* system manager configuration structure */
+typedef struct
+{
+ UINT16 mbox_evt; /* GKI mailbox event */
+ UINT8 mbox; /* GKI mailbox id */
+ UINT8 timer; /* GKI timer id */
+ UINT8 trace_level; /* initial trace level */
+} tBTA_SYS_CFG;
+
+/* data type to send events to BTA SYS HW manager */
+typedef struct
+{
+ BT_HDR hdr;
+ tBTA_SYS_HW_MODULE hw_module;
+} tBTA_SYS_HW_MSG;
+
+
+
+/*****************************************************************************
+** Global data
+*****************************************************************************/
+
+/* trace level */
+extern UINT8 appl_trace_level;
+
+/*****************************************************************************
+** Macros
+*****************************************************************************/
+
+/* Calculate start of event enumeration; id is top 8 bits of event */
+#define BTA_SYS_EVT_START(id) ((id) << 8)
+
+/*****************************************************************************
+** events for BTA SYS HW manager
+*****************************************************************************/
+
+/* events sent to SYS HW manager - must be kept synchronized with tables in bta_sys_main.c */
+enum
+{
+ /* device manager local device API events */
+ BTA_SYS_API_ENABLE_EVT = BTA_SYS_EVT_START(BTA_ID_SYS),
+ BTA_SYS_EVT_ENABLED_EVT,
+ BTA_SYS_EVT_STACK_ENABLED_EVT,
+ BTA_SYS_API_DISABLE_EVT,
+ BTA_SYS_EVT_DISABLED_EVT,
+ BTA_SYS_ERROR_EVT,
+
+ BTA_SYS_MAX_EVT
+};
+
+
+
+/* SYS HW status events - returned by SYS HW manager to other modules. */
+enum
+{
+ BTA_SYS_HW_OFF_EVT,
+ BTA_SYS_HW_ON_EVT,
+ BTA_SYS_HW_STARTING_EVT,
+ BTA_SYS_HW_STOPPING_EVT,
+ BTA_SYS_HW_ERROR_EVT
+
+};
+typedef UINT8 tBTA_SYS_HW_EVT;
+
+/* HW enable callback type */
+typedef void (tBTA_SYS_HW_CBACK)(tBTA_SYS_HW_EVT status);
+
+/*****************************************************************************
+** Function declarations
+*****************************************************************************/
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+BTA_API extern void bta_sys_init(void);
+BTA_API extern void bta_sys_event(BT_HDR *p_msg);
+BTA_API extern void bta_sys_timer_update(void);
+BTA_API extern void bta_sys_disable_timers(void);
+BTA_API extern void bta_sys_set_trace_level(UINT8 level);
+extern void bta_sys_register(UINT8 id, const tBTA_SYS_REG *p_reg);
+extern void bta_sys_deregister(UINT8 id);
+extern BOOLEAN bta_sys_is_register(UINT8 id);
+extern UINT16 bta_sys_get_sys_features(void);
+extern void bta_sys_sendmsg(void *p_msg);
+extern void bta_sys_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, INT32 timeout);
+extern void bta_sys_stop_timer(TIMER_LIST_ENT *p_tle);
+extern void bta_sys_disable(tBTA_SYS_HW_MODULE module);
+
+extern void bta_sys_hw_register( tBTA_SYS_HW_MODULE module, tBTA_SYS_HW_CBACK *cback);
+extern void bta_sys_hw_unregister( tBTA_SYS_HW_MODULE module );
+
+
+extern void bta_sys_rm_register(tBTA_SYS_CONN_CBACK * p_cback);
+extern void bta_sys_pm_register(tBTA_SYS_CONN_CBACK * p_cback);
+
+extern void bta_sys_policy_register(tBTA_SYS_CONN_CBACK * p_cback);
+extern void bta_sys_sco_register(tBTA_SYS_CONN_CBACK * p_cback);
+
+
+extern void bta_sys_conn_open(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
+extern void bta_sys_conn_close(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
+extern void bta_sys_app_open(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
+extern void bta_sys_app_close(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
+extern void bta_sys_sco_open(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
+extern void bta_sys_sco_close(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
+extern void bta_sys_sco_use(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
+extern void bta_sys_sco_unuse(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
+extern void bta_sys_idle(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
+extern void bta_sys_busy(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
+
+#if (BTM_SSR_INCLUDED == TRUE)
+extern void bta_sys_ssr_cfg_register(tBTA_SYS_SSR_CFG_CBACK * p_cback);
+extern void bta_sys_chg_ssr_config (UINT8 id, UINT8 app_id, UINT16 max_latency, UINT16 min_tout);
+#endif
+
+extern void bta_sys_role_chg_register(tBTA_SYS_CONN_CBACK * p_cback);
+extern void bta_sys_notify_role_chg(BD_ADDR_PTR p_bda, UINT8 new_role, UINT8 hci_status);
+extern void bta_sys_collision_register(UINT8 bta_id, tBTA_SYS_CONN_CBACK *p_cback);
+extern void bta_sys_notify_collision (BD_ADDR_PTR p_bda);
+
+#if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&(BTA_EIR_CANNED_UUID_LIST != TRUE)
+extern void bta_sys_eir_register(tBTA_SYS_EIR_CBACK * p_cback);
+extern void bta_sys_add_uuid(UINT16 uuid16);
+extern void bta_sys_remove_uuid(UINT16 uuid16);
+#else
+#define bta_sys_eir_register(ut)
+#define bta_sys_add_uuid(ut)
+#define bta_sys_remove_uuid(ut)
+#endif
+
+extern void bta_sys_set_policy (UINT8 id, UINT8 policy, BD_ADDR peer_addr);
+extern void bta_sys_clear_policy (UINT8 id, UINT8 policy, BD_ADDR peer_addr);
+extern void bta_sys_set_default_policy (UINT8 id, UINT8 policy);
+extern void bta_sys_clear_default_policy (UINT8 id, UINT8 policy);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* BTA_SYS_H */
diff --git a/bta/sys/bta_sys_cfg.c b/bta/sys/bta_sys_cfg.c
new file mode 100644
index 0000000..bcc7e40
--- /dev/null
+++ b/bta/sys/bta_sys_cfg.c
@@ -0,0 +1,55 @@
+/******************************************************************************
+ *
+ * Copyright (C) 2003-2012 Broadcom Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ * This file contains compile-time configurable constants for the BTA
+ * system manager.
+ *
+ ******************************************************************************/
+
+#include "bt_target.h"
+#include "gki.h"
+#include "bta_sys.h"
+
+/* GKI task mailbox event for BTA. */
+#ifndef BTA_MBOX_EVT
+#define BTA_MBOX_EVT TASK_MBOX_2_EVT_MASK
+#endif
+
+/* GKI task mailbox for BTA. */
+#ifndef BTA_MBOX
+#define BTA_MBOX TASK_MBOX_2
+#endif
+
+/* GKI timer id used for protocol timer for BTA. */
+#ifndef BTA_TIMER
+#define BTA_TIMER TIMER_1
+#endif
+
+const tBTA_SYS_CFG bta_sys_cfg =
+{
+ BTA_MBOX_EVT, /* GKI mailbox event */
+ BTA_MBOX, /* GKI mailbox id */
+ BTA_TIMER, /* GKI timer id */
+ APPL_INITIAL_TRACE_LEVEL /* initial trace level */
+};
+
+tBTA_SYS_CFG *p_bta_sys_cfg = (tBTA_SYS_CFG *)&bta_sys_cfg;
+
+
diff --git a/bta/sys/bta_sys_ci.c b/bta/sys/bta_sys_ci.c
new file mode 100644
index 0000000..d191077
--- /dev/null
+++ b/bta/sys/bta_sys_ci.c
@@ -0,0 +1,77 @@
+/******************************************************************************
+ *
+ * Copyright (C) 2010-2012 Broadcom Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ * This is the implementation file for BTA system call-in functions.
+ *
+ ******************************************************************************/
+
+#include "bta_sys.h"
+#include "bta_sys_ci.h"
+
+
+/*******************************************************************************
+**
+** Function bta_sys_hw_ci_enabled
+**
+** Description This function must be called in response to function
+** bta_sys_hw_enable_co(), when HW is indeed enabled
+**
+**
+** Returns void
+**
+*******************************************************************************/
+ void bta_sys_hw_ci_enabled(tBTA_SYS_HW_MODULE module )
+
+{
+ tBTA_SYS_HW_MSG *p_msg;
+
+ if ((p_msg = (tBTA_SYS_HW_MSG *) GKI_getbuf(sizeof(tBTA_SYS_HW_MSG))) != NULL)
+ {
+ p_msg->hdr.event = BTA_SYS_EVT_ENABLED_EVT;
+ p_msg->hw_module = module;
+
+ bta_sys_sendmsg(p_msg);
+ }
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_hw_ci_disabled
+**
+** Description This function must be called in response to function
+** bta_sys_hw_disable_co() when HW is really OFF
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_hw_ci_disabled( tBTA_SYS_HW_MODULE module )
+{
+ tBTA_SYS_HW_MSG *p_msg;
+
+ if ((p_msg = (tBTA_SYS_HW_MSG *) GKI_getbuf(sizeof(tBTA_SYS_HW_MSG))) != NULL)
+ {
+ p_msg->hdr.event = BTA_SYS_EVT_DISABLED_EVT;
+ p_msg->hw_module = module;
+
+ bta_sys_sendmsg(p_msg);
+ }
+}
+
diff --git a/bta/sys/bta_sys_conn.c b/bta/sys/bta_sys_conn.c
new file mode 100644
index 0000000..b495085
--- /dev/null
+++ b/bta/sys/bta_sys_conn.c
@@ -0,0 +1,577 @@
+/******************************************************************************
+ *
+ * Copyright (C) 2003-2012 Broadcom Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ * Routes connection status callbacks from various sub systems to DM
+ *
+ ******************************************************************************/
+
+#include "bta_api.h"
+#include "bta_sys.h"
+#include "bta_sys_int.h"
+#include "gki.h"
+
+
+/*******************************************************************************
+**
+** Function bta_sys_rm_register
+**
+** Description Called by BTA DM to register role management callbacks
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_rm_register(tBTA_SYS_CONN_CBACK * p_cback)
+{
+ bta_sys_cb.prm_cb = p_cback;
+}
+
+
+/*******************************************************************************
+**
+** Function bta_sys_policy_register
+**
+** Description Called by BTA DM to register link policy change callbacks
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_policy_register(tBTA_SYS_CONN_CBACK * p_cback)
+{
+ bta_sys_cb.p_policy_cb = p_cback;
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_role_chg_register
+**
+** Description Called by BTA AV to register role change callbacks
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_role_chg_register(tBTA_SYS_CONN_CBACK * p_cback)
+{
+ bta_sys_cb.p_role_cb = p_cback;
+}
+/*******************************************************************************
+**
+** Function bta_sys_ssr_cfg_register
+**
+** Description Called by BTA DM to register SSR configuration callback
+**
+**
+** Returns void
+**
+*******************************************************************************/
+#if (BTM_SSR_INCLUDED == TRUE)
+void bta_sys_ssr_cfg_register(tBTA_SYS_SSR_CFG_CBACK * p_cback)
+{
+ bta_sys_cb.p_ssr_cb = p_cback;
+}
+#endif
+/*******************************************************************************
+**
+** Function bta_sys_role_chg_register
+**
+** Description Called by BTA AV to register role change callbacks
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_notify_role_chg(BD_ADDR_PTR p_bda, UINT8 new_role, UINT8 hci_status)
+{
+ if (bta_sys_cb.p_role_cb)
+ {
+ bta_sys_cb.p_role_cb(BTA_SYS_ROLE_CHANGE, new_role, hci_status, p_bda);
+ }
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_collision_register
+**
+** Description Called by any BTA module to register for collision event.
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_collision_register(UINT8 bta_id, tBTA_SYS_CONN_CBACK *p_cback)
+{
+ UINT8 index;
+
+ for (index = 0; index < MAX_COLLISION_REG; index++)
+ {
+ if ((bta_sys_cb.colli_reg.id[index] == bta_id) ||
+ (bta_sys_cb.colli_reg.id[index] == 0))
+ {
+ bta_sys_cb.colli_reg.id[index] = bta_id;
+ bta_sys_cb.colli_reg.p_coll_cback[index] = p_cback;
+ return;
+ }
+ }
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_notify_collision
+**
+** Description Called by BTA DM to notify collision event.
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_notify_collision (BD_ADDR_PTR p_bda)
+{
+ UINT8 index;
+
+ for (index = 0; index < MAX_COLLISION_REG; index++)
+ {
+ if ((bta_sys_cb.colli_reg.id[index] != 0) &&
+ (bta_sys_cb.colli_reg.p_coll_cback[index] != NULL))
+ {
+ bta_sys_cb.colli_reg.p_coll_cback[index] (0, BTA_ID_SYS, 0, p_bda);
+ }
+ }
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_sco_register
+**
+** Description Called by BTA AV to register sco connection change callbacks
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_sco_register(tBTA_SYS_CONN_CBACK * p_cback)
+{
+ bta_sys_cb.p_sco_cb = p_cback;
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_pm_register
+**
+** Description Called by BTA DM to register power management callbacks
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_pm_register(tBTA_SYS_CONN_CBACK * p_cback)
+{
+ bta_sys_cb.ppm_cb = p_cback;
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_conn_open
+**
+** Description Called by BTA subsystems when a connection is made to
+** the service
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_conn_open(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
+{
+ if(bta_sys_cb.prm_cb)
+ {
+
+ bta_sys_cb.prm_cb(BTA_SYS_CONN_OPEN, id, app_id, peer_addr);
+
+ }
+
+ if(bta_sys_cb.ppm_cb)
+ {
+
+ bta_sys_cb.ppm_cb(BTA_SYS_CONN_OPEN, id, app_id, peer_addr);
+
+ }
+}
+
+
+
+/*******************************************************************************
+**
+** Function bta_sys_conn_close
+**
+** Description Called by BTA subsystems when a connection to the service
+** is closed
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_conn_close(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
+{
+ if(bta_sys_cb.prm_cb)
+ {
+
+ bta_sys_cb.prm_cb(BTA_SYS_CONN_CLOSE, id, app_id, peer_addr);
+
+ }
+
+ if(bta_sys_cb.ppm_cb)
+ {
+
+ bta_sys_cb.ppm_cb(BTA_SYS_CONN_CLOSE, id, app_id, peer_addr);
+
+ }
+}
+
+
+/*******************************************************************************
+**
+** Function bta_sys_app_open
+**
+** Description Called by BTA subsystems when application initiates connection
+** to a peer device
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_app_open(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
+{
+ if(bta_sys_cb.ppm_cb)
+ {
+ bta_sys_cb.ppm_cb(BTA_SYS_APP_OPEN, id, app_id, peer_addr);
+ }
+}
+
+
+
+/*******************************************************************************
+**
+** Function bta_sys_app_close
+**
+** Description Called by BTA subsystems when application initiates close
+** of connection to peer device
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_app_close(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
+{
+ if(bta_sys_cb.ppm_cb)
+ {
+ bta_sys_cb.ppm_cb(BTA_SYS_APP_CLOSE, id, app_id, peer_addr);
+ }
+}
+
+
+/*******************************************************************************
+**
+** Function bta_sys_sco_open
+**
+** Description Called by BTA subsystems when sco connection for that service
+** is open
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_sco_open(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
+{
+ /* AG triggers p_sco_cb by bta_sys_sco_use. */
+ if((id != BTA_ID_AG) && (bta_sys_cb.p_sco_cb))
+ {
+ /* without querying BTM_GetNumScoLinks() */
+ bta_sys_cb.p_sco_cb(BTA_SYS_SCO_OPEN, 1, app_id, peer_addr);
+ }
+
+ if(bta_sys_cb.ppm_cb)
+ {
+ bta_sys_cb.ppm_cb(BTA_SYS_SCO_OPEN, id, app_id, peer_addr);
+ }
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_sco_close
+**
+** Description Called by BTA subsystems when sco connection for that service
+** is closed
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_sco_close(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
+{
+ UINT8 num_sco_links;
+
+ if((id != BTA_ID_AG) && (bta_sys_cb.p_sco_cb))
+ {
+ num_sco_links = BTM_GetNumScoLinks();
+ bta_sys_cb.p_sco_cb(BTA_SYS_SCO_CLOSE, num_sco_links, app_id, peer_addr);
+ }
+
+ if(bta_sys_cb.ppm_cb)
+ {
+ bta_sys_cb.ppm_cb(BTA_SYS_SCO_CLOSE, id, app_id, peer_addr);
+ }
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_sco_use
+**
+** Description Called by BTA subsystems when that service needs to use sco.
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_sco_use(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
+{
+ /* AV streaming need to be suspended before SCO is connected. */
+ if(bta_sys_cb.p_sco_cb)
+ {
+ /* without querying BTM_GetNumScoLinks() */
+ bta_sys_cb.p_sco_cb(BTA_SYS_SCO_OPEN, 1, app_id, peer_addr);
+ }
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_sco_unuse
+**
+** Description Called by BTA subsystems when sco connection for that service
+** is no longer needed.
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_sco_unuse(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
+{
+ UINT8 num_sco_links;
+
+ if((bta_sys_cb.p_sco_cb))
+ {
+ num_sco_links = BTM_GetNumScoLinks();
+ bta_sys_cb.p_sco_cb(BTA_SYS_SCO_CLOSE, num_sco_links, app_id, peer_addr);
+ }
+}
+/*******************************************************************************
+**
+** Function bta_sys_chg_ssr_config
+**
+** Description Called by BTA subsystems to indicate that the given app SSR setting
+** need to be changed.
+**
+** Returns void
+**
+*******************************************************************************/
+#if (BTM_SSR_INCLUDED == TRUE)
+void bta_sys_chg_ssr_config (UINT8 id, UINT8 app_id, UINT16 max_latency, UINT16 min_tout)
+{
+ if(bta_sys_cb.p_ssr_cb)
+ {
+ bta_sys_cb.p_ssr_cb(id, app_id, max_latency, min_tout);
+ }
+}
+#endif
+/*******************************************************************************
+**
+** Function bta_sys_set_policy
+**
+** Description Called by BTA subsystems to indicate that the given link
+** policy to peer device should be set
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_set_policy (UINT8 id, UINT8 policy, BD_ADDR peer_addr)
+{
+ if(bta_sys_cb.p_policy_cb)
+ {
+ bta_sys_cb.p_policy_cb(BTA_SYS_PLCY_SET, id, policy, peer_addr);
+ }
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_clear_policy
+**
+** Description Called by BTA subsystems to indicate that the given link
+** policy to peer device should be clear
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_clear_policy (UINT8 id, UINT8 policy, BD_ADDR peer_addr)
+{
+ if(bta_sys_cb.p_policy_cb)
+ {
+ bta_sys_cb.p_policy_cb(BTA_SYS_PLCY_CLR, id, policy, peer_addr);
+ }
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_set_default_policy
+**
+** Description Called by BTA subsystems to indicate that the given default
+** link policy should be set
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_set_default_policy (UINT8 id, UINT8 policy)
+{
+ if(bta_sys_cb.p_policy_cb)
+ {
+ bta_sys_cb.p_policy_cb(BTA_SYS_PLCY_DEF_SET, id, policy, NULL);
+ }
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_clear_default_policy
+**
+** Description Called by BTA subsystems to indicate that the given default
+** link policy should be clear
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_clear_default_policy (UINT8 id, UINT8 policy)
+{
+ if(bta_sys_cb.p_policy_cb)
+ {
+ bta_sys_cb.p_policy_cb(BTA_SYS_PLCY_DEF_CLR, id, policy, NULL);
+ }
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_idle
+**
+** Description Called by BTA subsystems to indicate that the connection to
+** peer device is idle
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_idle(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
+{
+
+ if(bta_sys_cb.prm_cb)
+ {
+
+ bta_sys_cb.prm_cb(BTA_SYS_CONN_IDLE, id, app_id, peer_addr);
+
+ }
+
+ if(bta_sys_cb.ppm_cb)
+ {
+
+ bta_sys_cb.ppm_cb(BTA_SYS_CONN_IDLE, id, app_id, peer_addr);
+ }
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_busy
+**
+** Description Called by BTA subsystems to indicate that the connection to
+** peer device is busy
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_busy(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
+{
+ if(bta_sys_cb.prm_cb)
+ {
+
+ bta_sys_cb.prm_cb(BTA_SYS_CONN_BUSY, id, app_id, peer_addr);
+
+ }
+
+ if(bta_sys_cb.ppm_cb)
+ {
+
+ bta_sys_cb.ppm_cb(BTA_SYS_CONN_BUSY, id, app_id, peer_addr);
+
+ }
+}
+
+#if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&(BTA_EIR_CANNED_UUID_LIST != TRUE)
+/*******************************************************************************
+**
+** Function bta_sys_eir_register
+**
+** Description Called by BTA DM to register EIR utility function that can be
+** used by the other BTA modules to add/remove UUID.
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_eir_register(tBTA_SYS_EIR_CBACK * p_cback)
+{
+ bta_sys_cb.eir_cb = p_cback;
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_add_uuid
+**
+** Description Called by BTA subsystems to indicate to DM that new service
+** class UUID is added.
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_add_uuid(UINT16 uuid16)
+{
+ if(bta_sys_cb.eir_cb)
+ {
+ bta_sys_cb.eir_cb(uuid16, TRUE );
+ }
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_remove_uuid
+**
+** Description Called by BTA subsystems to indicate to DM that the service
+** class UUID is removed.
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_remove_uuid(UINT16 uuid16)
+{
+ if(bta_sys_cb.eir_cb)
+ {
+ bta_sys_cb.eir_cb(uuid16, FALSE);
+ }
+}
+#endif
+
diff --git a/bta/sys/bta_sys_int.h b/bta/sys/bta_sys_int.h
new file mode 100644
index 0000000..d187b29
--- /dev/null
+++ b/bta/sys/bta_sys_int.h
@@ -0,0 +1,119 @@
+/******************************************************************************
+ *
+ * Copyright (C) 2003-2012 Broadcom Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ * This is the private interface file for the BTA system manager.
+ *
+ ******************************************************************************/
+#ifndef BTA_SYS_INT_H
+#define BTA_SYS_INT_H
+
+#include "ptim.h"
+
+/*****************************************************************************
+** Constants and data types
+*****************************************************************************/
+
+/*****************************************************************************
+** state table
+*****************************************************************************/
+
+/* SYS HW state */
+enum
+{
+ BTA_SYS_HW_OFF,
+ BTA_SYS_HW_STARTING,
+ BTA_SYS_HW_ON,
+ BTA_SYS_HW_STOPPING
+};
+typedef UINT8 tBTA_SYS_HW_STATE;
+
+/* Collision callback */
+#define MAX_COLLISION_REG 5
+
+typedef struct
+{
+ UINT8 id[MAX_COLLISION_REG];
+ tBTA_SYS_CONN_CBACK *p_coll_cback[MAX_COLLISION_REG];
+} tBTA_SYS_COLLISION;
+
+/* system manager control block */
+typedef struct
+{
+ tBTA_SYS_REG *reg[BTA_ID_MAX]; /* registration structures */
+ BOOLEAN is_reg[BTA_ID_MAX]; /* registration structures */
+ tPTIM_CB ptim_cb; /* protocol timer list */
+ BOOLEAN timers_disabled; /* TRUE if sys timers disabled */
+ UINT8 task_id; /* GKI task id */
+ tBTA_SYS_HW_STATE state;
+ tBTA_SYS_HW_CBACK *sys_hw_cback[BTA_SYS_MAX_HW_MODULES]; /* enable callback for each HW modules */
+ UINT32 sys_hw_module_active; /* bitmask of all active modules */
+ UINT16 sys_features; /* Bitmask of sys features */
+
+ tBTA_SYS_CONN_CBACK *prm_cb; /* role management callback registered by DM */
+ tBTA_SYS_CONN_CBACK *ppm_cb; /* low power management callback registered by DM */
+ tBTA_SYS_CONN_CBACK *p_policy_cb; /* link policy change callback registered by DM */
+ tBTA_SYS_CONN_CBACK *p_sco_cb; /* SCO connection change callback registered by AV */
+ tBTA_SYS_CONN_CBACK *p_role_cb; /* role change callback registered by AV */
+ tBTA_SYS_COLLISION colli_reg; /* collision handling module */
+#if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&(BTA_EIR_CANNED_UUID_LIST != TRUE)
+ tBTA_SYS_EIR_CBACK *eir_cb; /* add/remove UUID into EIR */
+#endif
+#if (BTM_SSR_INCLUDED == TRUE)
+ tBTA_SYS_SSR_CFG_CBACK *p_ssr_cb;
+#endif
+} tBTA_SYS_CB;
+
+
+
+
+/*****************************************************************************
+** Global variables
+*****************************************************************************/
+
+/* system manager control block */
+#if BTA_DYNAMIC_MEMORY == FALSE
+extern tBTA_SYS_CB bta_sys_cb;
+#else
+extern tBTA_SYS_CB *bta_sys_cb_ptr;
+#define bta_sys_cb (*bta_sys_cb_ptr)
+#endif
+
+
+/* system manager configuration structure */
+extern tBTA_SYS_CFG *p_bta_sys_cfg;
+
+
+
+/* functions used for BTA SYS HW state machine */
+void bta_sys_hw_btm_cback( tBTM_DEV_STATUS status );
+void bta_sys_hw_error(tBTA_SYS_HW_MSG *p_sys_hw_msg);
+void bta_sys_hw_api_enable( tBTA_SYS_HW_MSG *p_sys_hw_msg );
+void bta_sys_hw_api_disable(tBTA_SYS_HW_MSG *p_sys_hw_msg);
+void bta_sys_hw_evt_enabled(tBTA_SYS_HW_MSG *p_sys_hw_msg);
+void bta_sys_hw_evt_disabled(tBTA_SYS_HW_MSG *p_sys_hw_msg);
+void bta_sys_hw_evt_stack_enabled(tBTA_SYS_HW_MSG *p_sys_hw_msg);
+
+BOOLEAN bta_sys_sm_execute(BT_HDR *p_msg);
+
+
+
+
+
+#endif /* BTA_SYS_INT_H */
diff --git a/bta/sys/bta_sys_main.c b/bta/sys/bta_sys_main.c
new file mode 100644
index 0000000..c1554bb
--- /dev/null
+++ b/bta/sys/bta_sys_main.c
@@ -0,0 +1,722 @@
+/******************************************************************************
+ *
+ * Copyright (C) 2003-2012 Broadcom Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ * This is the main implementation file for the BTA system manager.
+ *
+ ******************************************************************************/
+
+#include "btm_api.h"
+#include "bta_api.h"
+#include "bta_sys.h"
+#include "bta_sys_int.h"
+#include "bta_sys_ci.h"
+#include "bta_sys_co.h"
+#if BTA_FM_INCLUDED == TRUE
+#include "bta_fm_api.h"
+#endif
+#if BTA_FMTX_INCLUDED == TRUE
+#include "bta_fmtx_api.h"
+#endif
+#if GPS_INCLUDED == TRUE
+#include "bta_gps_api.h"
+#endif
+
+#include "gki.h"
+#include "ptim.h"
+#include <string.h>
+#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
+#include "bta_ar_api.h"
+#endif
+
+/* protocol timer update period, in milliseconds */
+#ifndef BTA_SYS_TIMER_PERIOD
+#define BTA_SYS_TIMER_PERIOD 1000
+#endif
+
+/* system manager control block definition */
+#if BTA_DYNAMIC_MEMORY == FALSE
+tBTA_SYS_CB bta_sys_cb;
+#endif
+
+/* trace level */
+/* TODO Bluedroid - Hard-coded trace levels - Needs to be configurable */
+UINT8 appl_trace_level = BT_TRACE_LEVEL_DEBUG; //APPL_INITIAL_TRACE_LEVEL;
+UINT8 btif_trace_level = BT_TRACE_LEVEL_DEBUG;
+
+static const tBTA_SYS_REG bta_sys_hw_reg =
+{
+ bta_sys_sm_execute,
+ NULL
+};
+
+
+/* type for action functions */
+typedef void (*tBTA_SYS_ACTION)(tBTA_SYS_HW_MSG *p_data);
+
+/* action function list */
+const tBTA_SYS_ACTION bta_sys_action[] =
+{
+ /* device manager local device API events - cf bta_sys.h for events */
+ bta_sys_hw_api_enable, /* 0 BTA_SYS_HW_API_ENABLE_EVT */
+ bta_sys_hw_evt_enabled, /* 1 BTA_SYS_HW_EVT_ENABLED_EVT */
+ bta_sys_hw_evt_stack_enabled, /* 2 BTA_SYS_HW_EVT_STACK_ENABLED_EVT */
+ bta_sys_hw_api_disable, /* 3 BTA_SYS_HW_API_DISABLE_EVT */
+ bta_sys_hw_evt_disabled, /* 4 BTA_SYS_HW_EVT_DISABLED_EVT */
+ bta_sys_hw_error /* 5 BTA_SYS_HW_ERROR_EVT */
+};
+
+/* state machine action enumeration list */
+enum
+{
+ /* device manager local device API events */
+ BTA_SYS_HW_API_ENABLE,
+ BTA_SYS_HW_EVT_ENABLED,
+ BTA_SYS_HW_EVT_STACK_ENABLED,
+ BTA_SYS_HW_API_DISABLE,
+ BTA_SYS_HW_EVT_DISABLED,
+ BTA_SYS_HW_ERROR
+};
+
+#define BTA_SYS_NUM_ACTIONS (BTA_SYS_MAX_EVT & 0x00ff)
+#define BTA_SYS_IGNORE BTA_SYS_NUM_ACTIONS
+
+/* state table information */
+#define BTA_SYS_ACTIONS 2 /* number of actions */
+#define BTA_SYS_NEXT_STATE 2 /* position of next state */
+#define BTA_SYS_NUM_COLS 3 /* number of columns in state tables */
+
+
+/* state table for OFF state */
+const UINT8 bta_sys_hw_off[][BTA_SYS_NUM_COLS] =
+{
+/* Event Action 1 Action 2 Next State */
+/* API_ENABLE */ {BTA_SYS_HW_API_ENABLE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING},
+/* EVT_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING},
+/* STACK_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
+/* API_DISABLE */ {BTA_SYS_HW_EVT_DISABLED, BTA_SYS_IGNORE, BTA_SYS_HW_OFF},
+/* EVT_DISABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_OFF},
+/* EVT_ERROR */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_OFF}
+};
+
+const UINT8 bta_sys_hw_starting[][BTA_SYS_NUM_COLS] =
+{
+/* Event Action 1 Action 2 Next State */
+/* API_ENABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING}, /* wait for completion event */
+/* EVT_ENABLED */ {BTA_SYS_HW_EVT_ENABLED, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING},
+/* STACK_ENABLED */ {BTA_SYS_HW_EVT_STACK_ENABLED, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
+/* API_DISABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STOPPING}, /* successive disable/enable: change state wait for completion to disable */
+/* EVT_DISABLED */ {BTA_SYS_HW_EVT_DISABLED, BTA_SYS_HW_API_ENABLE, BTA_SYS_HW_STARTING}, /* successive enable/disable: notify, then restart HW */
+/* EVT_ERROR */ {BTA_SYS_HW_ERROR, BTA_SYS_IGNORE, BTA_SYS_HW_ON}
+};
+
+const UINT8 bta_sys_hw_on[][BTA_SYS_NUM_COLS] =
+{
+/* Event Action 1 Action 2 Next State */
+/* API_ENABLE */ {BTA_SYS_HW_API_ENABLE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
+/* EVT_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
+/* STACK_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
+/* API_DISABLE */ {BTA_SYS_HW_API_DISABLE, BTA_SYS_IGNORE, BTA_SYS_HW_ON}, /* don't change the state here, as some other modules might be active */
+/* EVT_DISABLED */ {BTA_SYS_HW_ERROR, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
+/* EVT_ERROR */ {BTA_SYS_HW_ERROR, BTA_SYS_IGNORE, BTA_SYS_HW_ON}
+};
+
+const UINT8 bta_sys_hw_stopping[][BTA_SYS_NUM_COLS] =
+{
+/* Event Action 1 Action 2 Next State */
+/* API_ENABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING}, /* change state, and wait for completion event to enable */
+/* EVT_ENABLED */ {BTA_SYS_HW_EVT_ENABLED, BTA_SYS_IGNORE, BTA_SYS_HW_STOPPING}, /* successive enable/disable: finish the enable before disabling */
+/* STACK_ENABLED */ {BTA_SYS_HW_EVT_STACK_ENABLED, BTA_SYS_HW_API_DISABLE, BTA_SYS_HW_STOPPING}, /* successive enable/disable: notify, then stop */
+/* API_DISABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STOPPING}, /* wait for completion event */
+/* EVT_DISABLED */ {BTA_SYS_HW_EVT_DISABLED, BTA_SYS_IGNORE, BTA_SYS_HW_OFF},
+/* EVT_ERROR */ {BTA_SYS_HW_API_DISABLE, BTA_SYS_IGNORE, BTA_SYS_HW_STOPPING}
+};
+
+typedef const UINT8 (*tBTA_SYS_ST_TBL)[BTA_SYS_NUM_COLS];
+
+/* state table */
+const tBTA_SYS_ST_TBL bta_sys_st_tbl[] = {
+ bta_sys_hw_off,
+ bta_sys_hw_starting,
+ bta_sys_hw_on,
+ bta_sys_hw_stopping
+};
+
+/*******************************************************************************
+**
+** Function bta_sys_init
+**
+** Description BTA initialization; called from task initialization.
+**
+**
+** Returns void
+**
+*******************************************************************************/
+BTA_API void bta_sys_init(void)
+{
+ memset(&bta_sys_cb, 0, sizeof(tBTA_SYS_CB));
+ ptim_init(&bta_sys_cb.ptim_cb, BTA_SYS_TIMER_PERIOD, p_bta_sys_cfg->timer);
+ bta_sys_cb.task_id = GKI_get_taskid();
+ appl_trace_level = p_bta_sys_cfg->trace_level;
+
+ /* register BTA SYS message handler */
+ bta_sys_register( BTA_ID_SYS, &bta_sys_hw_reg);
+
+ /* register for BTM notifications */
+ BTM_RegisterForDeviceStatusNotif ((tBTM_DEV_STATUS_CB*)&bta_sys_hw_btm_cback );
+
+#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
+ bta_ar_init();
+#endif
+
+}
+
+/*******************************************************************************
+**
+** Function bta_dm_sm_execute
+**
+** Description State machine event handling function for DM
+**
+**
+** Returns void
+**
+*******************************************************************************/
+BOOLEAN bta_sys_sm_execute(BT_HDR *p_msg)
+{
+ BOOLEAN freebuf = TRUE;
+ tBTA_SYS_ST_TBL state_table;
+ UINT8 action;
+ int i;
+
+ APPL_TRACE_EVENT2("bta_sys_sm_execute state:%d, event:0x%x", bta_sys_cb.state, p_msg->event);
+
+ /* look up the state table for the current state */
+ state_table = bta_sys_st_tbl[bta_sys_cb.state];
+ /* update state */
+ bta_sys_cb.state = state_table[p_msg->event & 0x00ff][BTA_SYS_NEXT_STATE];
+
+ /* execute action functions */
+ for (i = 0; i < BTA_SYS_ACTIONS; i++)
+ {
+ if ((action = state_table[p_msg->event & 0x00ff][i]) != BTA_SYS_IGNORE)
+ {
+ (*bta_sys_action[action])( (tBTA_SYS_HW_MSG*) p_msg);
+ }
+ else
+ {
+ break;
+ }
+ }
+ return freebuf;
+
+}
+
+
+void bta_sys_hw_register( tBTA_SYS_HW_MODULE module, tBTA_SYS_HW_CBACK *cback)
+{
+ bta_sys_cb.sys_hw_cback[module]=cback;
+}
+
+
+void bta_sys_hw_unregister( tBTA_SYS_HW_MODULE module )
+{
+ bta_sys_cb.sys_hw_cback[module]=NULL;
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_hw_btm_cback
+**
+** Description This function is registered by BTA SYS to BTM in order to get status notifications
+**
+**
+** Returns
+**
+*******************************************************************************/
+void bta_sys_hw_btm_cback( tBTM_DEV_STATUS status )
+{
+
+ tBTA_SYS_HW_MSG *sys_event;
+
+ APPL_TRACE_DEBUG1(" bta_sys_hw_btm_cback was called with parameter: %i" , status );
+
+ /* send a message to BTA SYS */
+ if ((sys_event = (tBTA_SYS_HW_MSG *) GKI_getbuf(sizeof(tBTA_SYS_HW_MSG))) != NULL)
+ {
+ if (status == BTM_DEV_STATUS_UP)
+ sys_event->hdr.event = BTA_SYS_EVT_STACK_ENABLED_EVT;
+ else if (status == BTM_DEV_STATUS_DOWN)
+ sys_event->hdr.event = BTA_SYS_ERROR_EVT;
+ else
+ {
+ /* BTM_DEV_STATUS_CMD_TOUT is ignored for now. */
+ GKI_freebuf (sys_event);
+ sys_event = NULL;
+ }
+
+ if (sys_event)
+ {
+ bta_sys_sendmsg(sys_event);
+ }
+ }
+ else
+ {
+ APPL_TRACE_DEBUG0("ERROR bta_sys_hw_btm_cback couldn't send msg" );
+ }
+}
+
+
+
+/*******************************************************************************
+**
+** Function bta_sys_hw_error
+**
+** Description In case the HW device stops answering... Try to turn it off, then re-enable all
+** previously active SW modules.
+**
+** Returns success or failure
+**
+*******************************************************************************/
+void bta_sys_hw_error(tBTA_SYS_HW_MSG *p_sys_hw_msg)
+{
+
+ UINT8 module_index;
+
+ APPL_TRACE_DEBUG1("%s", __FUNCTION__);
+
+ for (module_index = 0; module_index < BTA_SYS_MAX_HW_MODULES; module_index++)
+ {
+ if( bta_sys_cb.sys_hw_module_active & ((UINT32)1 << module_index )) {
+ switch( module_index)
+ {
+ case BTA_SYS_HW_BLUETOOTH:
+ /* Send BTA_SYS_HW_ERROR_EVT to DM */
+ if (bta_sys_cb.sys_hw_cback[module_index] != NULL)
+ bta_sys_cb.sys_hw_cback[module_index] (BTA_SYS_HW_ERROR_EVT);
+ break;
+ default:
+ /* not yet supported */
+ break;
+ }
+ }
+ }
+}
+
+
+
+/*******************************************************************************
+**
+** Function bta_sys_hw_enable
+**
+** Description this function is called after API enable and HW has been turned on
+**
+**
+** Returns success or failure
+**
+*******************************************************************************/
+
+void bta_sys_hw_api_enable( tBTA_SYS_HW_MSG *p_sys_hw_msg )
+{
+ if ((!bta_sys_cb.sys_hw_module_active) && (bta_sys_cb.state != BTA_SYS_HW_ON))
+ {
+ /* register which HW module was turned on */
+ bta_sys_cb.sys_hw_module_active |= ((UINT32)1 << p_sys_hw_msg->hw_module );
+
+ /* use call-out to power-up HW */
+ bta_sys_hw_co_enable(p_sys_hw_msg->hw_module);
+ }
+ else
+ {
+ /* register which HW module was turned on */
+ bta_sys_cb.sys_hw_module_active |= ((UINT32)1 << p_sys_hw_msg->hw_module );
+
+ /* HW already in use, so directly notify the caller */
+ if (bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module ]!= NULL )
+ bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module ]( BTA_SYS_HW_ON_EVT );
+ }
+
+ APPL_TRACE_EVENT2 ("bta_sys_hw_api_enable for %d, active modules 0x%04X",
+ p_sys_hw_msg->hw_module, bta_sys_cb.sys_hw_module_active);
+
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_hw_disable
+**
+** Description if no other module is using the HW, this function will call ( if defined ) a user-macro to turn off the HW
+**
+**
+** Returns success or failure
+**
+*******************************************************************************/
+void bta_sys_hw_api_disable(tBTA_SYS_HW_MSG *p_sys_hw_msg)
+{
+ APPL_TRACE_DEBUG2("bta_sys_hw_api_disable for %d, active modules: 0x%04X",
+ p_sys_hw_msg->hw_module, bta_sys_cb.sys_hw_module_active );
+
+ /* make sure the related SW blocks were stopped */
+ bta_sys_disable( p_sys_hw_msg->hw_module );
+
+
+ /* register which module we turn off */
+ bta_sys_cb.sys_hw_module_active &= ~((UINT32)1 << p_sys_hw_msg->hw_module );
+
+
+ /* if there are still some SW modules using the HW, just provide an answer to the calling */
+ if( bta_sys_cb.sys_hw_module_active != 0 )
+ {
+ /* if there are still some SW modules using the HW, directly notify the caller */
+ if( bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module ]!= NULL )
+ bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module ]( BTA_SYS_HW_OFF_EVT );
+ }
+ else
+ {
+ /* manually update the state of our system */
+ bta_sys_cb.state = BTA_SYS_HW_STOPPING;
+ /* and use the call-out to disable HW */
+ bta_sys_hw_co_disable(p_sys_hw_msg->hw_module);
+ }
+
+}
+
+
+/*******************************************************************************
+**
+** Function bta_sys_hw_event_enabled
+**
+** Description
+**
+**
+** Returns success or failure
+**
+*******************************************************************************/
+void bta_sys_hw_evt_enabled(tBTA_SYS_HW_MSG *p_sys_hw_msg)
+{
+ APPL_TRACE_EVENT1("bta_sys_hw_evt_enabled for %i", p_sys_hw_msg->hw_module);
+
+#if ( defined BTM_AUTOMATIC_HCI_RESET && BTM_AUTOMATIC_HCI_RESET == TRUE )
+ /* If device is already up, send a fake "BTM DEVICE UP" using BTA SYS state machine */
+ /* If we are in the middle device initialization, BTM_DEVICE_UP will be issued */
+ /* by BTM once initialization is done. */
+ if (BTA_DmIsDeviceUp())
+ {
+ bta_sys_hw_btm_cback (BTM_DEV_STATUS_UP);
+ }
+#else
+
+ /* if HCI reset was not sent during stack start-up */
+ BTM_DeviceReset( NULL );
+
+#endif
+}
+
+
+/*******************************************************************************
+**
+** Function bta_sys_hw_event_disabled
+**
+** Description
+**
+**
+** Returns success or failure
+**
+*******************************************************************************/
+void bta_sys_hw_evt_disabled(tBTA_SYS_HW_MSG *p_sys_hw_msg)
+{
+ UINT8 hw_module_index;
+
+ APPL_TRACE_DEBUG1("bta_sys_hw_evt_disabled - module 0x%X", p_sys_hw_msg->hw_module);
+
+ for (hw_module_index = 0; hw_module_index < BTA_SYS_MAX_HW_MODULES; hw_module_index++)
+ {
+ if (bta_sys_cb.sys_hw_cback[hw_module_index] != NULL)
+ bta_sys_cb.sys_hw_cback[hw_module_index] (BTA_SYS_HW_OFF_EVT);
+ }
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_hw_event_stack_enabled
+**
+** Description we receive this event from once the SW side is ready ( stack, FW download,... ),
+** i.e. we can really start using the device. So notify the app.
+**
+** Returns success or failure
+**
+*******************************************************************************/
+void bta_sys_hw_evt_stack_enabled(tBTA_SYS_HW_MSG *p_sys_hw_msg)
+{
+ UINT8 hw_module_index;
+
+ APPL_TRACE_DEBUG0(" bta_sys_hw_evt_stack_enabled!notify the callers");
+
+ for (hw_module_index = 0; hw_module_index < BTA_SYS_MAX_HW_MODULES; hw_module_index++ )
+ {
+ if (bta_sys_cb.sys_hw_cback[hw_module_index] != NULL)
+ bta_sys_cb.sys_hw_cback[hw_module_index] (BTA_SYS_HW_ON_EVT);
+ }
+}
+
+
+
+
+/*******************************************************************************
+**
+** Function bta_sys_event
+**
+** Description BTA event handler; called from task event handler.
+**
+**
+** Returns void
+**
+*******************************************************************************/
+BTA_API void bta_sys_event(BT_HDR *p_msg)
+{
+ UINT8 id;
+ BOOLEAN freebuf = TRUE;
+
+ APPL_TRACE_EVENT1("BTA got event 0x%x", p_msg->event);
+
+ /* get subsystem id from event */
+ id = (UINT8) (p_msg->event >> 8);
+
+ /* verify id and call subsystem event handler */
+ if ((id < BTA_ID_MAX) && (bta_sys_cb.reg[id] != NULL))
+ {
+ freebuf = (*bta_sys_cb.reg[id]->evt_hdlr)(p_msg);
+ }
+ else
+ {
+ APPL_TRACE_WARNING1("BTA got unregistered event id %d", id);
+ }
+
+ if (freebuf)
+ {
+ GKI_freebuf(p_msg);
+ }
+
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_timer_update
+**
+** Description Update the BTA timer list and handle expired timers.
+**
+** Returns void
+**
+*******************************************************************************/
+BTA_API void bta_sys_timer_update(void)
+{
+ if (!bta_sys_cb.timers_disabled)
+ {
+ ptim_timer_update(&bta_sys_cb.ptim_cb);
+ }
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_register
+**
+** Description Called by other BTA subsystems to register their event
+** handler.
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_register(UINT8 id, const tBTA_SYS_REG *p_reg)
+{
+ bta_sys_cb.reg[id] = (tBTA_SYS_REG *) p_reg;
+ bta_sys_cb.is_reg[id] = TRUE;
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_deregister
+**
+** Description Called by other BTA subsystems to de-register
+** handler.
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_deregister(UINT8 id)
+{
+ bta_sys_cb.is_reg[id] = FALSE;
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_is_register
+**
+** Description Called by other BTA subsystems to get registeration
+** status.
+**
+**
+** Returns void
+**
+*******************************************************************************/
+BOOLEAN bta_sys_is_register(UINT8 id)
+{
+ return bta_sys_cb.is_reg[id];
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_sendmsg
+**
+** Description Send a GKI message to BTA. This function is designed to
+** optimize sending of messages to BTA. It is called by BTA
+** API functions and call-in functions.
+**
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_sendmsg(void *p_msg)
+{
+ GKI_send_msg(bta_sys_cb.task_id, p_bta_sys_cfg->mbox, p_msg);
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_start_timer
+**
+** Description Start a protocol timer for the specified amount
+** of time in milliseconds.
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, INT32 timeout)
+{
+ ptim_start_timer(&bta_sys_cb.ptim_cb, p_tle, type, timeout);
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_stop_timer
+**
+** Description Stop a BTA timer.
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_stop_timer(TIMER_LIST_ENT *p_tle)
+{
+ ptim_stop_timer(&bta_sys_cb.ptim_cb, p_tle);
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_disable
+**
+** Description For each registered subsystem execute its disable function.
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_disable(tBTA_SYS_HW_MODULE module)
+{
+ int bta_id = 0;
+ int bta_id_max = 0;
+
+ APPL_TRACE_DEBUG1("bta_sys_disable: module %i", module);
+
+ switch( module )
+ {
+ case BTA_SYS_HW_BLUETOOTH:
+ bta_id = BTA_ID_DM;
+ bta_id_max = BTA_ID_BLUETOOTH_MAX;
+ break;
+ case BTA_SYS_HW_FMRX:
+ bta_id = BTA_ID_FM;
+ bta_id_max = BTA_ID_FM;
+ break;
+ case BTA_SYS_HW_FMTX:
+ bta_id = BTA_ID_FMTX;
+ bta_id_max = BTA_ID_FMTX;
+ break;
+ case BTA_SYS_HW_GPS:
+ bta_id = BTA_ID_GPS;
+ bta_id_max = BTA_ID_GPS;
+ break;
+ default:
+ APPL_TRACE_WARNING0("bta_sys_disable: unkown module");
+ return;
+ }
+
+ for ( ; bta_id <= bta_id_max; bta_id++)
+ {
+ if (bta_sys_cb.reg[bta_id] != NULL)
+ {
+ if (bta_sys_cb.is_reg[bta_id] == TRUE && bta_sys_cb.reg[bta_id]->disable != NULL)
+ {
+ (*bta_sys_cb.reg[bta_id]->disable)();
+ }
+ }
+ }
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_disable_timers
+**
+** Description Disable sys timer event handling
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_disable_timers(void)
+{
+ bta_sys_cb.timers_disabled = TRUE;
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_set_trace_level
+**
+** Description Set trace level for BTA
+**
+** Returns void
+**
+*******************************************************************************/
+void bta_sys_set_trace_level(UINT8 level)
+{
+ appl_trace_level = level;
+}
+
+/*******************************************************************************
+**
+** Function bta_sys_get_sys_features
+**
+** Description Returns sys_features to other BTA modules.
+**
+** Returns sys_features
+**
+*******************************************************************************/
+UINT16 bta_sys_get_sys_features (void)
+{
+ return bta_sys_cb.sys_features;
+}
+
+
diff --git a/bta/sys/ptim.c b/bta/sys/ptim.c
new file mode 100644
index 0000000..9dffc66
--- /dev/null
+++ b/bta/sys/ptim.c
@@ -0,0 +1,162 @@
+/******************************************************************************
+ *
+ * Copyright (C) 2003-2012 Broadcom Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ * Protocol timer services.
+ *
+ ******************************************************************************/
+
+#include "bt_target.h"
+#include "gki.h"
+#include "ptim.h"
+#include "bta_sys.h"
+
+/*******************************************************************************
+**
+** Function ptim_init
+**
+** Description Initialize a protocol timer control block. Parameter
+** period is the GKI timer period in milliseconds. Parameter
+** timer_id is the GKI timer id.
+**
+** Returns void
+**
+*******************************************************************************/
+void ptim_init(tPTIM_CB *p_cb, UINT16 period, UINT8 timer_id)
+{
+ GKI_init_timer_list(&p_cb->timer_queue);
+ p_cb->period = period;
+ p_cb->timer_id = timer_id;
+}
+
+/*******************************************************************************
+**
+** Function ptim_timer_update
+**
+** Description Update the protocol timer list and handle expired timers.
+** This function is called from the task running the protocol
+** timers when the periodic GKI timer expires.
+**
+** Returns void
+**
+*******************************************************************************/
+void ptim_timer_update(tPTIM_CB *p_cb)
+{
+ TIMER_LIST_ENT *p_tle;
+ BT_HDR *p_msg;
+ UINT32 new_ticks_count;
+ INT32 period_in_ticks;
+
+ /* To handle the case when the function is called less frequently than the period
+ we must convert determine the number of ticks since the last update, then
+ convert back to milliseconds before updating timer list */
+ new_ticks_count = GKI_get_tick_count();
+
+ /* Check for wrapped condition */
+ if (new_ticks_count >= p_cb->last_gki_ticks)
+ {
+ period_in_ticks = (INT32)(new_ticks_count - p_cb->last_gki_ticks);
+ }
+ else
+ {
+ period_in_ticks = (INT32)(((UINT32)0xffffffff - p_cb->last_gki_ticks)
+ + new_ticks_count + 1);
+ }
+
+ /* update timer list */
+ GKI_update_timer_list(&p_cb->timer_queue, GKI_TICKS_TO_MS(period_in_ticks));
+
+ p_cb->last_gki_ticks = new_ticks_count;
+
+ /* while there are expired timers */
+ while((p_cb->timer_queue.p_first) && (p_cb->timer_queue.p_first->ticks <= 0))
+ {
+ /* removed expired timer from list */
+ p_tle = p_cb->timer_queue.p_first;
+ GKI_remove_from_timer_list(&p_cb->timer_queue, p_tle);
+
+ /* call timer callback */
+ if(p_tle->p_cback)
+ {
+ (*p_tle->p_cback)(p_tle);
+ }
+ else if(p_tle->event)
+ {
+ if ((p_msg = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
+ {
+ p_msg->event = p_tle->event;
+ p_msg->layer_specific = 0;
+ bta_sys_sendmsg(p_msg);
+ }
+ }
+ }
+
+ /* if timer list is empty stop periodic GKI timer */
+ if (p_cb->timer_queue.p_first == NULL)
+ {
+ GKI_stop_timer(p_cb->timer_id);
+ }
+}
+
+/*******************************************************************************
+**
+** Function ptim_start_timer
+**
+** Description Start a protocol timer for the specified amount
+** of time in seconds.
+**
+** Returns void
+**
+*******************************************************************************/
+void ptim_start_timer(tPTIM_CB *p_cb, TIMER_LIST_ENT *p_tle, UINT16 type, INT32 timeout)
+{
+ /* if timer list is currently empty, start periodic GKI timer */
+ if (p_cb->timer_queue.p_first == NULL)
+ {
+ p_cb->last_gki_ticks = GKI_get_tick_count();
+ GKI_start_timer(p_cb->timer_id, GKI_MS_TO_TICKS(p_cb->period), TRUE);
+ }
+
+ GKI_remove_from_timer_list(&p_cb->timer_queue, p_tle);
+
+ p_tle->event = type;
+ p_tle->ticks = timeout;
+
+ GKI_add_to_timer_list(&p_cb->timer_queue, p_tle);
+}
+
+/*******************************************************************************
+**
+** Function ptim_stop_timer
+**
+** Description Stop a protocol timer.
+**
+** Returns void
+**
+*******************************************************************************/
+void ptim_stop_timer(tPTIM_CB *p_cb, TIMER_LIST_ENT *p_tle)
+{
+ GKI_remove_from_timer_list (&p_cb->timer_queue, p_tle);
+
+ /* if timer list is empty stop periodic GKI timer */
+ if (p_cb->timer_queue.p_first == NULL)
+ {
+ GKI_stop_timer(p_cb->timer_id);
+ }
+}
diff --git a/bta/sys/utl.c b/bta/sys/utl.c
new file mode 100644
index 0000000..4bb1d95
--- /dev/null
+++ b/bta/sys/utl.c
@@ -0,0 +1,296 @@
+/******************************************************************************
+ *
+ * Copyright (C) 2003-2012 Broadcom Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ * This file contains utility functions.
+ *
+ ******************************************************************************/
+#include "utl.h"
+#include "gki.h"
+#include "btm_api.h"
+
+/*******************************************************************************
+**
+** Function utl_str2int
+**
+** Description This utility function converts a character string to an
+** integer. Acceptable values in string are 0-9. If invalid
+** string or string value too large, -1 is returned. Leading
+** spaces are skipped.
+**
+**
+** Returns Integer value or -1 on error.
+**
+*******************************************************************************/
+INT16 utl_str2int(const char *p_s)
+{
+ INT32 val = 0;
+
+ for (;*p_s == ' ' && *p_s != 0; p_s++);
+
+ if (*p_s == 0) return -1;
+
+ for (;;)
+ {
+ if ((*p_s < '0') || (*p_s > '9')) return -1;
+
+ val += (INT32) (*p_s++ - '0');
+
+ if (val > 32767) return -1;
+
+ if (*p_s == 0)
+ {
+ return (INT16) val;
+ }
+ else
+ {
+ val *= 10;
+ }
+ }
+}
+
+/*******************************************************************************
+**
+** Function utl_strucmp
+**
+** Description This utility function compares two strings in uppercase.
+** String p_s must be uppercase. String p_t is converted to
+** uppercase if lowercase. If p_s ends first, the substring
+** match is counted as a match.
+**
+**
+** Returns 0 if strings match, nonzero otherwise.
+**
+*******************************************************************************/
+int utl_strucmp(const char *p_s, const char *p_t)
+{
+ char c;
+
+ while (*p_s && *p_t)
+ {
+ c = *p_t++;
+ if (c >= 'a' && c <= 'z')
+ {
+ c -= 0x20;
+ }
+ if (*p_s++ != c)
+ {
+ return -1;
+ }
+ }
+ /* if p_t hit null first, no match */
+ if (*p_t == 0 && *p_s != 0)
+ {
+ return 1;
+ }
+ /* else p_s hit null first, count as match */
+ else
+ {
+ return 0;
+ }
+}
+
+/*******************************************************************************
+**
+** Function utl_itoa
+**
+** Description This utility function converts a UINT16 to a string. The
+** string is NULL-terminated. The length of the string is
+** returned;
+**
+**
+** Returns Length of string.
+**
+*******************************************************************************/
+UINT8 utl_itoa(UINT16 i, char *p_s)
+{
+ UINT16 j, k;
+ char *p = p_s;
+ BOOLEAN fill = FALSE;
+
+ if (i == 0)
+ {
+ /* take care of zero case */
+ *p++ = '0';
+ }
+ else
+ {
+ for(j = 10000; j > 0; j /= 10)
+ {
+ k = i / j;
+ i %= j;
+ if (k > 0 || fill)
+ {
+ *p++ = k + '0';
+ fill = TRUE;
+ }
+ }
+ }
+ *p = 0;
+ return (UINT8) (p - p_s);
+}
+
+/*******************************************************************************
+**
+** Function utl_freebuf
+**
+** Description This function calls GKI_freebuf to free the buffer passed
+** in, if buffer pointer is not NULL, and also initializes
+** buffer pointer to NULL.
+**
+**
+** Returns Nothing.
+**
+*******************************************************************************/
+void utl_freebuf(void **p)
+{
+ if (*p != NULL)
+ {
+ GKI_freebuf(*p);
+ *p = NULL;
+ }
+}
+
+
+/*******************************************************************************
+**
+** Function utl_set_device_class
+**
+** Description This function updates the local Device Class.
+**
+** Parameters:
+** p_cod - Pointer to the device class to set to
+**
+** cmd - the fields of the device class to update.
+** BTA_UTL_SET_COD_MAJOR_MINOR, - overwrite major, minor class
+** BTA_UTL_SET_COD_SERVICE_CLASS - set the bits in the input
+** BTA_UTL_CLR_COD_SERVICE_CLASS - clear the bits in the input
+** BTA_UTL_SET_COD_ALL - overwrite major, minor, set the bits in service class
+** BTA_UTL_INIT_COD - overwrite major, minor, and service class
+**
+** Returns TRUE if successful, Otherwise FALSE
+**
+*******************************************************************************/
+BOOLEAN utl_set_device_class(tBTA_UTL_COD *p_cod, UINT8 cmd)
+{
+ UINT8 *dev;
+ UINT16 service;
+ UINT8 minor, major;
+ DEV_CLASS dev_class;
+
+ dev = BTM_ReadDeviceClass();
+ BTM_COD_SERVICE_CLASS( service, dev );
+ BTM_COD_MINOR_CLASS(minor, dev );
+ BTM_COD_MAJOR_CLASS(major, dev );
+
+ switch(cmd)
+ {
+ case BTA_UTL_SET_COD_MAJOR_MINOR:
+ minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
+ major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
+ break;
+
+ case BTA_UTL_SET_COD_SERVICE_CLASS:
+ /* clear out the bits that is not SERVICE_CLASS bits */
+ p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
+ service = service | p_cod->service;
+ break;
+
+ case BTA_UTL_CLR_COD_SERVICE_CLASS:
+ p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
+ service = service & (~p_cod->service);
+ break;
+
+ case BTA_UTL_SET_COD_ALL:
+ minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
+ major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
+ p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
+ service = service | p_cod->service;
+ break;
+
+ case BTA_UTL_INIT_COD:
+ minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
+ major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
+ service = p_cod->service & BTM_COD_SERVICE_CLASS_MASK;
+ break;
+
+ default:
+ return FALSE;
+ }
+
+ /* convert the fields into the device class type */
+ FIELDS_TO_COD(dev_class, minor, major, service);
+
+ if (BTM_SetDeviceClass(dev_class) == BTM_SUCCESS)
+ return TRUE;
+
+ return FALSE;
+}
+
+/*******************************************************************************
+**
+** Function utl_isintstr
+**
+** Description This utility function checks if the given string is an
+** integer string or not
+**
+**
+** Returns TRUE if successful, Otherwise FALSE
+**
+*******************************************************************************/
+BOOLEAN utl_isintstr(const char *p_s)
+{
+ UINT16 i = 0;
+
+ for(i=0; p_s[i] != 0; i++)
+ {
+ if(((p_s[i] < '0') || (p_s[i] > '9')) && (p_s[i] != ';'))
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/*******************************************************************************
+**
+** Function utl_isdialstr
+**
+** Description This utility function checks if the given string contains
+** only dial digits or not
+**
+**
+** Returns TRUE if successful, Otherwise FALSE
+**
+*******************************************************************************/
+BOOLEAN utl_isdialstr(const char *p_s)
+{
+ UINT16 i = 0;
+
+ for(i=0; p_s[i] != 0; i++)
+ {
+ if(!(((p_s[i] >= '0') && (p_s[i] <= '9'))
+ || (p_s[i] == '*') || (p_s[i] == '+') || (p_s[i] == '#') || (p_s[i] == ';')
+ || ((p_s[i] >= 'A') && (p_s[i] <= 'C'))))
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+