Android TV Reference Remote
Check in SDK based on Telink 8278 chipset, solution provided
by Telink Semiconductor (Shanghai) Co., Ltd.

This source code drop contains the open source software. The libraries,
compiler toolchain and other binaries dependencies are hosted by Telink.

ATTENTION:

Please read the “readme” file in the root directory after downloading
this SDK for instructions on getting/setting up the tools, libraries,
and binaries necessary for building the SDK, and all the reference design files.

Software version: 2.02

v2.02 Library version

|       lib name        |          origin           |
| :-------------------: | :-----------------------: |
| libfirmware_encrypt.a |      ble SDK V3.4.2       |
|     liblt_827x.a      |      ble SDK V3.4.2       |
|     libapp_ota.a      | google BLE RCU SDK v2.00  |
| liblt_general_stack.a |      ble SDK v3.4.2       |

v2.02 Changes
  - Fixed ir over ble suppress issue (ble mode <-> ir mode)
  - Added rcu RPA function (The public address is used for normal pairing.
    If the peer uses rpa, rcu will use the rpa address when re-connect.)

BYPASS_INCLUSIVE_LANGUAGE_REASON=tech_terms.
No-Typo-Check: name in lib.

Change-Id: Idb04339028cbd1d18349fea083ff115ffcb984e7
diff --git a/tl_check_fw.sh b/tl_check_fw.sh
index 3d3e626..a4020bc 100755
--- a/tl_check_fw.sh
+++ b/tl_check_fw.sh
@@ -7,5 +7,4 @@
 tc32-elf-objcopy -v -O binary $2.elf $1.bin
 ../check_fw  $1.bin
 ../FW_SIG_ENC 3 $1.bin FW_ENC_KEY
-
 echo "**************** end of post build ******************"
diff --git a/vendor/827x_ble_remote/app.c b/vendor/827x_ble_remote/app.c
index 9d2c06c..6711451 100644
--- a/vendor/827x_ble_remote/app.c
+++ b/vendor/827x_ble_remote/app.c
@@ -255,6 +255,7 @@
 
 void app_adv_interval_downgrade_direct(void)
 {
+    u8 status=0;
     smp_param_save_t  bondInfo;
     u8 bond_number = blc_smp_param_getCurrentBondingDeviceNumber();  //get bonded device number
     //direct adv
@@ -263,11 +264,23 @@
        printf("app_adv_internal_downgrade_direct\r\n");
        bls_smp_param_loadByIndex( bond_number - 1, &bondInfo);  //get the latest bonding device (index: bond_number-1 )
 
-       u8 status = bls_ll_setAdvParam( ADV_INTERVAL_30MS, ADV_INTERVAL_35MS,
+       if (memcmp(bondInfo.peer_id_addr, bondInfo.peer_addr, 6) == 0)
+       {
+         status = bls_ll_setAdvParam( ADV_INTERVAL_30MS, ADV_INTERVAL_35MS,
                                         ADV_TYPE_CONNECTABLE_DIRECTED_LOW_DUTY, app_own_address_type,
                                         bondInfo.peer_addr_type,  bondInfo.peer_addr,
                                         MY_APP_ADV_CHANNEL,
                                         ADV_FP_NONE);
+       }
+       else
+       {
+           app_own_address_type = OWN_ADDRESS_RESOLVE_PRIVATE_PUBLIC;
+           status = bls_ll_setAdvParam( ADV_INTERVAL_30MS, ADV_INTERVAL_35MS,
+              ADV_TYPE_CONNECTABLE_DIRECTED_LOW_DUTY, app_own_address_type,
+              bondInfo.peer_id_adrType,  bondInfo.peer_id_addr,
+                                                 MY_APP_ADV_CHANNEL,
+                                                 ADV_FP_NONE);
+       }
        if(status != BLE_SUCCESS) {      while(1); }  //debug: adv setting err
     }
 }
@@ -317,6 +330,7 @@
 
 void app_adv_direct(void)
 {
+    u8 status=0;
     printf("app_adv_direct\r\n");
     is_reconn_mode = 0x55;
     smp_param_save_t  bondInfo;
@@ -327,11 +341,30 @@
        app_set_adv_interval_downgrade_direct();
        bls_smp_param_loadByIndex( bond_number - 1, &bondInfo);  //get the latest bonding device (index: bond_number-1 )
 
-       u8 status = bls_ll_setAdvParam( MY_ADV_INTERVAL_MIN, MY_ADV_INTERVAL_MAX,
+       if (memcmp(bondInfo.peer_id_addr, bondInfo.peer_addr, 6) == 0)
+       {
+           printf("host not support rpa\r\n");
+           status = bls_ll_setAdvParam( MY_ADV_INTERVAL_MIN, MY_ADV_INTERVAL_MAX,
                                         ADV_TYPE_CONNECTABLE_DIRECTED_LOW_DUTY, app_own_address_type,
                                         bondInfo.peer_addr_type,  bondInfo.peer_addr,
                                         MY_APP_ADV_CHANNEL,
                                         ADV_FP_NONE);
+       }
+       else
+       {
+           printf("host support rpa\r\n");
+           ll_resolvingList_reset();
+           status = ll_resolvingList_add(bondInfo.peer_id_adrType, bondInfo.peer_id_addr, bondInfo.peer_irk, bondInfo.local_irk);
+           printf("LL resolving list add status: 0x%x\n", status);
+           status = ll_resolvingList_setAddrResolutionEnable(1);
+           printf("LL add resolution enable status: 0x%x\n", status);
+           app_own_address_type = OWN_ADDRESS_RESOLVE_PRIVATE_PUBLIC;
+           status = bls_ll_setAdvParam( MY_ADV_INTERVAL_MIN, MY_ADV_INTERVAL_MAX,
+                ADV_TYPE_CONNECTABLE_DIRECTED_LOW_DUTY, app_own_address_type,
+                bondInfo.peer_id_adrType,  bondInfo.peer_id_addr,
+                                                   MY_APP_ADV_CHANNEL,
+                                                   ADV_FP_NONE);
+       }
        if(status != BLE_SUCCESS) {      while(1); }  //debug: adv setting err
        bls_ll_setAdvEnable(1);  //adv enable
     }
@@ -558,7 +591,7 @@
         google_voice_ctl = 0;
 #endif
     }
-
+    ir_suppress_reinit();
     device_in_connection_state = 0;
     mtuExchange_check_tick = 0;
     flag_dle = 0;
@@ -1135,6 +1168,7 @@
         app_own_address_type = OWN_ADDRESS_RANDOM;
         blc_ll_setRandomAddr(mac_random_static);
     #endif
+    blc_ll_resolvListInit();
 
     ////// Controller Initialization  //////////
     blc_ll_initBasicMCU();                      //mandatory
@@ -1237,30 +1271,40 @@
     {
         u8 bond_number = blc_smp_param_getCurrentBondingDeviceNumber();  //get bonded device number
         smp_param_save_t  bondInfo;
-        if(bond_number)   //at least 1 bonding device exist
+        if(bond_number)   //set direct adv
         {
             bls_smp_param_loadByIndex( bond_number - 1, &bondInfo);  //get the latest bonding device (index: bond_number-1 )
             ll_resolvingList_reset();
             ll_resolvingList_add(bondInfo.peer_id_adrType,bondInfo.peer_id_addr,bondInfo.peer_irk,bondInfo.local_irk);
-        }
-
-        if(bond_number)   //set direct adv
-        {
             app_set_adv_interval_downgrade_direct();
             //printf("\r\npeer_addr=%x %x %x %x %x %x\r\n",bondInfo.peer_addr[0],bondInfo.peer_addr[1],bondInfo.peer_addr[2],bondInfo.peer_addr[3],bondInfo.peer_addr[4],bondInfo.peer_addr[5]);
             //printf("\r\npeer_id_addr=%x %x %x %x %x %x\r\n",bondInfo.peer_id_addr[0],bondInfo.peer_id_addr[1],bondInfo.peer_id_addr[2],bondInfo.peer_id_addr[3],bondInfo.peer_id_addr[4],bondInfo.peer_id_addr[5]);
+            enterdeep_time = ADV_RECONN_ENTER_DEEP_COUNT;
             if((analog_read(USED_DEEP_ANA_REG) & OTA_FLG) == OTA_FLG)
             {
                 printf("ana read = ota succ\r\n");
                 service_change = 0x55;
             }
-            enterdeep_time = ADV_RECONN_ENTER_DEEP_COUNT;
-            //set direct adv
-            u8 status = bls_ll_setAdvParam( MY_ADV_INTERVAL_MIN, MY_ADV_INTERVAL_MAX,
+            u8 status=0;
+            if (memcmp(bondInfo.peer_id_addr, bondInfo.peer_addr, 6) == 0)
+            {
+                printf("host not support rpa\r\n");
+                status = bls_ll_setAdvParam( MY_ADV_INTERVAL_MIN, MY_ADV_INTERVAL_MAX,
                                             ADV_TYPE_CONNECTABLE_DIRECTED_LOW_DUTY, app_own_address_type,
                                             bondInfo.peer_addr_type,  bondInfo.peer_addr,
                                             MY_APP_ADV_CHANNEL,
                                             ADV_FP_NONE);
+            }
+            else
+            {
+                printf("host support rpa\r\n");
+                app_own_address_type = OWN_ADDRESS_RESOLVE_PRIVATE_PUBLIC;
+                status = bls_ll_setAdvParam( MY_ADV_INTERVAL_MIN, MY_ADV_INTERVAL_MAX,
+                        ADV_TYPE_CONNECTABLE_DIRECTED_LOW_DUTY, app_own_address_type,
+                        bondInfo.peer_id_adrType,  bondInfo.peer_id_addr,
+                                                           MY_APP_ADV_CHANNEL,
+                                                           ADV_FP_NONE);
+            }
             if(status != BLE_SUCCESS) {      while(1); }  //debug: adv setting err
             //it is recommended that direct adv only last for several seconds, then switch to indirect adv
             //bls_ll_setAdvDuration(MY_DIRECT_ADV_TMIE, 1);
diff --git a/vendor/827x_ble_remote/app_ir.c b/vendor/827x_ble_remote/app_ir.c
index 0ba9dc0..a1b50b7 100644
--- a/vendor/827x_ble_remote/app_ir.c
+++ b/vendor/827x_ble_remote/app_ir.c
@@ -942,7 +942,14 @@
     else
         return false;
 }
-
+void ir_suppress_reinit(void)
+{
+    u8 i;
+    for (i = 0; i < MAX_KEY_COUNT; i++)
+    {
+        ir_app_parm.ir_suppress[i] = 0;
+    }
+}
 /**
  * @brief       ir_type_read
  * @param[in]   none
@@ -1383,7 +1390,7 @@
     u8 index,data;
   //  u8 testdata[6],i;
   //  u32 addr;
-    //u8 k;
+    u8 k;
 
     switch (handle)
     {
@@ -1513,20 +1520,33 @@
         }
         case ATV_IR_SUPPRESS_IDX:
         {
-            if(len == 2)
+            if(len)
             {
-                ir_app_parm.ir_programming.current_programming_key_id = buf[0];
-                ir_app_parm.ir_programming.current_programming_key_id <<= 8;
-                ir_app_parm.ir_programming.current_programming_key_id |= buf[1];
-                index = ir_get_suppress_index_from_keyid_idx(ir_app_parm.ir_programming.current_programming_key_id);
-                if(ir_app_parm.ir_suppress[index])
+                printf("switch to ble\r\n");
+                if(len%2 == 0)
+            {
+                    for(k=0;k<len/2;k++)
+                    {
+                        u16 id = (buf[2*k]<<8) | buf[2*k+1];
+                        index = ir_get_button_idx_from_key_id(id);
+                        printf("index=%x\r\n",index);
+                        if(ir_app_parm.ir_suppress[index] == 0)
                 {
-                    ir_app_parm.ir_suppress[index] = 0;
+                            ir_app_parm.ir_suppress[index] = 1;
                 }
+                    }
+            }
+        }
                 else
-                    ir_app_parm.ir_suppress[index] = 1;
-                ir_app_parm.ir_programming.current_programming_key_id = INVALID_KEY_ID;
-
+            {
+                printf("switch to ir\r\n");
+                for(k=0;k<MAX_KEY_COUNT;k++)
+                {
+                    if(ir_app_parm.ir_suppress[k])
+                    {
+                        ir_app_parm.ir_suppress[k] = 0;
+                    }
+                }
             }
             break;
         }
diff --git a/vendor/827x_ble_remote/app_ir.h b/vendor/827x_ble_remote/app_ir.h
index 0449d26..30c46cb 100644
--- a/vendor/827x_ble_remote/app_ir.h
+++ b/vendor/827x_ble_remote/app_ir.h
@@ -71,5 +71,6 @@
 extern void ir_nec_ir_table_erase_loop(void);
 extern void ir_flash_erase_loop(void);
 extern void app_custom_set_new_ir_table(void);
+extern void ir_suppress_reinit(void);
 #endif
 #endif
diff --git a/vendor/827x_ble_remote/rc_ir.c b/vendor/827x_ble_remote/rc_ir.c
index 0e40047..79856af 100644
--- a/vendor/827x_ble_remote/rc_ir.c
+++ b/vendor/827x_ble_remote/rc_ir.c
@@ -315,7 +315,6 @@
                     printf("nec ir irq release\r\n");
                     reg_pwm_irq_mask &= ~FLD_IRQ_PWM0_IR_DMA_FIFO_DONE; //disable irq mask
                     ir_send_ctrl.is_sending = IR_SENDING_NONE;
- 
                 }
             }
             else{
diff --git a/version.h b/version.h
index 6381244..c1a7671 100644
--- a/version.h
+++ b/version.h
@@ -27,6 +27,6 @@
 #ifndef VERSION_H_
 #define VERSION_H_
 
-#define FW_VERSION                  0x11110200//FW:v3.4.2  SW:v2.00
+#define FW_VERSION                  0x11110202//FW:v3.4.2  SW:v2.02
 
 #endif /* VERSION_H_ */