| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2012, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: $ |
|---|
| 11 | * $brcm_Revision: $ |
|---|
| 12 | * $brcm_Date: $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: tuner test module |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: $ |
|---|
| 19 | * |
|---|
| 20 | * |
|---|
| 21 | ***************************************************************************/ |
|---|
| 22 | #include "bstd.h" |
|---|
| 23 | #include "bkni.h" |
|---|
| 24 | #include "bdbg.h" |
|---|
| 25 | |
|---|
| 26 | #include "serial.h" |
|---|
| 27 | #include "input_parser.h" |
|---|
| 28 | #include "bcrc32.h" |
|---|
| 29 | |
|---|
| 30 | #include "bsettop.h" |
|---|
| 31 | #include "bsettop_tuner.h" |
|---|
| 32 | #include "bsettop_smessage.h" |
|---|
| 33 | |
|---|
| 34 | BDBG_MODULE(test_tuner); |
|---|
| 35 | |
|---|
| 36 | #define MAX_MSG_BUFFERS 0x400 |
|---|
| 37 | #define MSG_BUFFER_SIZE 0x1000 |
|---|
| 38 | |
|---|
| 39 | unsigned char filter_msg_buffer[MSG_BUFFER_SIZE]; |
|---|
| 40 | |
|---|
| 41 | void *filter_msg_callback(void *context, size_t data_size) |
|---|
| 42 | { |
|---|
| 43 | uint8_t *msg; |
|---|
| 44 | uint32_t crc, i; |
|---|
| 45 | |
|---|
| 46 | msg = (uint8_t*)filter_msg_buffer; |
|---|
| 47 | /* check syntax indicator to see if we need to do crc */ |
|---|
| 48 | if(0 == (msg[1] & 0x80)){ |
|---|
| 49 | crc = bcrc32(msg, data_size); |
|---|
| 50 | if(0 != crc){ |
|---|
| 51 | BDBG_ERR(("crc error detected")); |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | BDBG_ERR(("msg %hx size %u", msg[0], data_size)); |
|---|
| 55 | |
|---|
| 56 | //#define DUMP_MSG |
|---|
| 57 | #ifdef DUMP_MSG |
|---|
| 58 | for (i = 0; i < 16; i++) { |
|---|
| 59 | BKNI_Printf("[%3d]=0x%02x\n", i, msg[i]); |
|---|
| 60 | } |
|---|
| 61 | #endif |
|---|
| 62 | |
|---|
| 63 | return msg; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | static btuner_t tuner = NULL; |
|---|
| 67 | static btuner_params tuner_params; |
|---|
| 68 | static smessage_stream_t filter_msg = NULL; |
|---|
| 69 | static unsigned int freq_khz = 549000; /* 549 MHz */ |
|---|
| 70 | |
|---|
| 71 | bool qam_tuner_init(void) |
|---|
| 72 | { |
|---|
| 73 | if (tuner) |
|---|
| 74 | return true; |
|---|
| 75 | |
|---|
| 76 | tuner = btuner_open(0); |
|---|
| 77 | if (NULL == tuner) { |
|---|
| 78 | BKNI_Printf("btuner_open failed"); |
|---|
| 79 | return false; |
|---|
| 80 | } |
|---|
| 81 | return true; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | bool qam_tune(unsigned int freq) |
|---|
| 85 | { |
|---|
| 86 | if (NULL == tuner) { |
|---|
| 87 | BKNI_Printf("tuner is not initialized"); |
|---|
| 88 | return false; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | btuner_params_init(&tuner_params, tuner); |
|---|
| 92 | tuner_params.wait_for_lock = false; |
|---|
| 93 | tuner_params.fecMode = eFEC_ANNEX_B; |
|---|
| 94 | tuner_params.qamMode = eQAM_Scan; |
|---|
| 95 | |
|---|
| 96 | freq_khz = freq / 1000; |
|---|
| 97 | if (btuner_tune(tuner, freq_khz * 1000, &tuner_params) < 0) { |
|---|
| 98 | BKNI_Printf("btuner_tune failed\n"); |
|---|
| 99 | return false; |
|---|
| 100 | } |
|---|
| 101 | return true; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | bool qam_get_status(void) |
|---|
| 105 | { |
|---|
| 106 | btuner_status status; |
|---|
| 107 | |
|---|
| 108 | if (NULL == tuner) { |
|---|
| 109 | BKNI_Printf("tuner is not initialized"); |
|---|
| 110 | return false; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | btuner_get_status(tuner, &status); |
|---|
| 114 | BKNI_Printf("tuner is %s on frequency %d KHz\n", status.lock ? "locked" : "not locked", freq_khz); |
|---|
| 115 | return true; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | bool qam_start_message(unsigned short pid) |
|---|
| 119 | { |
|---|
| 120 | bresult bres; |
|---|
| 121 | smessage_stream_params_t params; |
|---|
| 122 | |
|---|
| 123 | /* if started already */ |
|---|
| 124 | if (filter_msg) |
|---|
| 125 | return true; |
|---|
| 126 | |
|---|
| 127 | filter_msg = smessage_open(smessage_format_psi); |
|---|
| 128 | if (NULL == filter_msg) { |
|---|
| 129 | BKNI_Printf("smessage_open failed"); |
|---|
| 130 | return false; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | smessage_stream_params_init(¶ms, NULL); |
|---|
| 134 | params.band = TUNER_BAND; |
|---|
| 135 | params.pid = pid; |
|---|
| 136 | |
|---|
| 137 | params.buffer = filter_msg_buffer; |
|---|
| 138 | params.buffer_size = MSG_BUFFER_SIZE; |
|---|
| 139 | params.crc_disabled = true; |
|---|
| 140 | params.data_ready_callback = filter_msg_callback; |
|---|
| 141 | params.overflow = NULL; |
|---|
| 142 | //params.priv = (void *)4; |
|---|
| 143 | |
|---|
| 144 | bres = smessage_start(¶ms, filter_msg); |
|---|
| 145 | if (b_ok != bres) |
|---|
| 146 | { |
|---|
| 147 | BDBG_ERR(("%s:%d",__FILE__, __LINE__)); |
|---|
| 148 | smessage_close(filter_msg); |
|---|
| 149 | filter_msg = NULL; |
|---|
| 150 | return false; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | return true; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | bool qam_stop_message(void) |
|---|
| 157 | { |
|---|
| 158 | if (!filter_msg) { |
|---|
| 159 | BKNI_Printf("message filtering is not started yet"); |
|---|
| 160 | return false; |
|---|
| 161 | } |
|---|
| 162 | smessage_stop(filter_msg); |
|---|
| 163 | smessage_close(filter_msg); |
|---|
| 164 | filter_msg = NULL; |
|---|
| 165 | |
|---|
| 166 | return true; |
|---|
| 167 | } |
|---|