Move utils lib out from implementation namespace.

RadioService may benefit from it too.

Also, fix subchannel base, as I found a tiny comment about it in the 1.0 HAL.

Bug: b/32621193
Test: instrumentalization
Change-Id: I11939025b72bdeab4cc6393e25159f53164e22ed
diff --git a/broadcastradio/1.1/default/Tuner.cpp b/broadcastradio/1.1/default/Tuner.cpp
index a36ec3f..3c43c2e 100644
--- a/broadcastradio/1.1/default/Tuner.cpp
+++ b/broadcastradio/1.1/default/Tuner.cpp
@@ -98,7 +98,7 @@
     ProgramInfo info11 = {};
     auto& info10 = info11.base;
 
-    utils::getLegacyChannel(selector, info10.channel, info10.subChannel);
+    utils::getLegacyChannel(selector, &info10.channel, &info10.subChannel);
     info11.selector = selector;
     info11.flags |= ProgramInfoFlags::MUTED;
 
diff --git a/broadcastradio/1.1/default/VirtualProgram.cpp b/broadcastradio/1.1/default/VirtualProgram.cpp
index ef8bd1c..babf0d8 100644
--- a/broadcastradio/1.1/default/VirtualProgram.cpp
+++ b/broadcastradio/1.1/default/VirtualProgram.cpp
@@ -31,7 +31,7 @@
     ProgramInfo info11 = {};
     auto& info10 = info11.base;
 
-    utils::getLegacyChannel(selector, info10.channel, info10.subChannel);
+    utils::getLegacyChannel(selector, &info10.channel, &info10.subChannel);
     info11.selector = selector;
     info10.tuned = true;
     info10.stereo = true;
diff --git a/broadcastradio/1.1/types.hal b/broadcastradio/1.1/types.hal
index dee38b8..004184f 100644
--- a/broadcastradio/1.1/types.hal
+++ b/broadcastradio/1.1/types.hal
@@ -80,7 +80,15 @@
     DAB,     // Digital audio broadcasting
     DRMO,    // Digital Radio Mondiale
     SXM,     // SiriusXM Satellite Radio
-    VENDOR,  // vendor-specific, not synced across devices
+
+    /**
+     * Vendor-specific, not synced across devices.
+     *
+     * If it's necessary to support multiple vendor-specific program types, the
+     * type of vendor radio technology may be encoded in high bits
+     * of IDENTIFIER_TYPE_VENDOR_PRIMARY.
+     */
+    VENDOR,
 };
 
 /**
diff --git a/broadcastradio/1.1/utils/Utils.cpp b/broadcastradio/1.1/utils/Utils.cpp
index 9dc0a53..270aabb 100644
--- a/broadcastradio/1.1/utils/Utils.cpp
+++ b/broadcastradio/1.1/utils/Utils.cpp
@@ -24,7 +24,6 @@
 namespace hardware {
 namespace broadcastradio {
 namespace V1_1 {
-namespace implementation {
 namespace utils {
 
 using V1_0::Band;
@@ -166,26 +165,31 @@
     sel.primaryId.type = static_cast<uint32_t>(IdentifierType::AMFM_FREQUENCY);
     sel.primaryId.value = channel;
     if (subChannel > 0) {
-        // stating sub channel for AM/FM channel does not give any guarantees,
-        // but we can't do much more without HD station ID
+        /* stating sub channel for AM/FM channel does not give any guarantees,
+         * but we can't do much more without HD station ID
+         *
+         * The legacy APIs had 1-based subChannels, while ProgramSelector is 0-based.
+         */
         sel.secondaryIds = hidl_vec<ProgramIdentifier>{
-            {static_cast<uint32_t>(IdentifierType::HD_SUBCHANNEL), subChannel},
+            {static_cast<uint32_t>(IdentifierType::HD_SUBCHANNEL), subChannel - 1},
         };
     }
 
     return sel;
 }
 
-bool getLegacyChannel(const ProgramSelector& sel, uint32_t& channelOut, uint32_t& subChannelOut) {
+bool getLegacyChannel(const ProgramSelector& sel, uint32_t* channelOut, uint32_t* subChannelOut) {
+    if (channelOut) *channelOut = 0;
+    if (subChannelOut) *subChannelOut = 0;
     if (isAmFm(getType(sel))) {
-        channelOut = getId(sel, IdentifierType::AMFM_FREQUENCY);
-        subChannelOut = getId(sel, IdentifierType::HD_SUBCHANNEL, 0);
+        if (channelOut) *channelOut = getId(sel, IdentifierType::AMFM_FREQUENCY);
+        if (subChannelOut && hasId(sel, IdentifierType::HD_SUBCHANNEL)) {
+            // The legacy APIs had 1-based subChannels, while ProgramSelector is 0-based.
+            *subChannelOut = getId(sel, IdentifierType::HD_SUBCHANNEL) + 1;
+        }
         return true;
-    } else {
-        channelOut = 0;
-        subChannelOut = 0;
-        return false;
     }
+    return false;
 }
 
 bool isDigital(const ProgramSelector& sel) {
@@ -200,7 +204,6 @@
 }
 
 }  // namespace utils
-}  // namespace implementation
 }  // namespace V1_1
 }  // namespace broadcastradio
 }  // namespace hardware
diff --git a/broadcastradio/1.1/utils/Utils.h b/broadcastradio/1.1/utils/Utils.h
index 1110e79..cd86ffa 100644
--- a/broadcastradio/1.1/utils/Utils.h
+++ b/broadcastradio/1.1/utils/Utils.h
@@ -25,7 +25,6 @@
 namespace hardware {
 namespace broadcastradio {
 namespace V1_1 {
-namespace implementation {
 namespace utils {
 
 /**
@@ -61,12 +60,11 @@
 
 ProgramSelector make_selector(V1_0::Band band, uint32_t channel, uint32_t subChannel = 0);
 
-bool getLegacyChannel(const ProgramSelector& sel, uint32_t& channelOut, uint32_t& subChannelOut);
+bool getLegacyChannel(const ProgramSelector& sel, uint32_t* channelOut, uint32_t* subChannelOut);
 
 bool isDigital(const ProgramSelector& sel);
 
 }  // namespace utils
-}  // namespace implementation
 }  // namespace V1_1
 }  // namespace broadcastradio
 }  // namespace hardware