Skip to contentSkip to conversation

cec-follower: add tuner analog service emulation

PATCH Superseded c0d1n61at3 at gmail.com (Jiunn Chang) 4 participants 14 messages Sep 17, 2019 – Sep 24, 2019 Media (V4L/DVB)

lore
Share & tools
lore

c0d1n61at3 at gmail.com (Jiunn Chang)

#0 lore raw

PATCH 1/1 cec-follower: add tuner analog service emulation

Implement the following tuner control features:
- <Select Analogue Service>
- <Give Tuner Device Status> and reply <Tuner Device Status>

Signed-off-by Jiunn Chang <c0d1n61at3 at gmail.com>
---
 utils/cec-follower/cec-tuner.cpp | 51 ++++++++++++++++++++++++--------
 1 file changed, 39 insertions(+), 12 deletions(-)
utils/cec-follower/cec-tuner.cpp +39 -12
index 912adcb9..ed198ac8 100644
@@ -87,6 +87,28 @@ static unsigned int analog_freqs_khz[3][9][3] =
 	}
 };
 
+static struct cec_op_tuner_device_info tuner_dev_info = {};
+
+static void set_analog_channel_freq(const unsigned int ana_freq_khz) {
+	tuner_dev_info.analog.ana_freq = (ana_freq_khz * 10) / 625;
+}
+
+static bool set_analog_tuner_dev_info(const struct cec_msg *msg)
+{
+	tuner_dev_info.rec_flag = CEC_OP_REC_FLAG_NOT_USED;
+	tuner_dev_info.tuner_display_info = CEC_OP_TUNER_DISPLAY_INFO_ANALOGUE;
+	tuner_dev_info.is_analog = true;
+	cec_ops_select_analogue_service(msg,
+					&tuner_dev_info.analog.ana_bcast_type,
+					&tuner_dev_info.analog.ana_freq,
+					&tuner_dev_info.analog.bcast_system);
+	if (tuner_dev_info.analog.ana_bcast_type > 2 ||
+	    tuner_dev_info.analog.bcast_system > 8)
+		return false;
+	set_analog_channel_freq(analog_freqs_khz[1][tuner_dev_info.analog.bcast_system][tuner_dev_info.analog.ana_bcast_type]);
+	return true;
+}
+
 void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, unsigned me)
 {
 	switch (msg.msg[1]) {
@@ -101,29 +123,34 @@ void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns
 		*/
 
 	case CEC_MSG_GIVE_TUNER_DEVICE_STATUS: {
-		if (!cec_has_tuner(1 << me))
+		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
 			break;
 
-		struct cec_op_tuner_device_info tuner_dev_info = {};
-
 		cec_msg_set_reply_to(&msg, &msg);
-		tuner_dev_info.rec_flag = CEC_OP_REC_FLAG_NOT_USED;
-		tuner_dev_info.tuner_display_info = CEC_OP_TUNER_DISPLAY_INFO_NONE;
-		tuner_dev_info.is_analog = false;
-		tuner_dev_info.digital.service_id_method = CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL;
-		tuner_dev_info.digital.dig_bcast_system = CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_C;
-		tuner_dev_info.digital.channel.channel_number_fmt = CEC_OP_CHANNEL_NUMBER_FMT_1_PART;
-		tuner_dev_info.digital.channel.minor = 1;
-
 		cec_msg_tuner_device_status(&msg, &tuner_dev_info);
 		transmit(node, &msg);
 		return;
 	}
-
 	case CEC_MSG_TUNER_DEVICE_STATUS:
 		return;
 
 	case CEC_MSG_SELECT_ANALOGUE_SERVICE:
+		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
+			break;
+
+		if (tuner_dev_info.rec_flag == CEC_OP_REC_FLAG_USED) {
+			msg.msg[1] = CEC_MSG_FEATURE_ABORT;
+			msg.msg[2] = CEC_OP_ABORT_REFUSED;
+			transmit(node, &msg);
+			return;
+		}
+		if (!set_analog_tuner_dev_info(&msg)) {
+			msg.msg[1] = CEC_MSG_FEATURE_ABORT;
+			msg.msg[2] = CEC_OP_ABORT_INVALID_OP;
+			transmit(node, &msg);
+			return;
+		}
+		return;
 	case CEC_MSG_SELECT_DIGITAL_SERVICE:
 	case CEC_MSG_TUNER_STEP_DECREMENT:
 	case CEC_MSG_TUNER_STEP_INCREMENT:
Signature · 1 line
2.23.0
#1 lore raw

PATCH 1/1 cec-follower: add tuner analog service emulation

Quoted 14 lines · Jiunn Chang

Implement the following tuner control features:
- <Select Analogue Service>
- <Give Tuner Device Status> and reply <Tuner Device Status>

Signed-off-by Jiunn Chang <c0d1n61at3 at gmail.com>
---
 utils/cec-follower/cec-tuner.cpp | 51 ++++++++++++++++++++++++--------
 1 file changed, 39 insertions(+), 12 deletions(-)
utils/cec-follower/cec-tuner.cpp +0 -0
index 912adcb9..ed198ac8 100644

utils/cec-follower/cec-tuner.cpp static unsigned int analog_freqs_khz[3][9][3] = · line 90

Quoted 4 lines
utils/cec-follower/cec-tuner.cpp +1 -0
@@ -87,6 +87,28 @@ static unsigned int analog_freqs_khz[3][9][3] =
 	}
 };
 
+static struct cec_op_tuner_device_info tuner_dev_info = {};

This shouldn't be a global, instead add it to struct state (cec-follower.h). That way the complete state of the follower is in a single place.

utils/cec-follower/cec-tuner.cpp static unsigned int analog_freqs_khz[3][9][3] = · line 92

Quoted 2 lines
utils/cec-follower/cec-tuner.cpp +2 -0
+
+static void set_analog_channel_freq(const unsigned int ana_freq_khz) {

It is never needed to add const to an int argument.

And { should be on the next line.

Quoted 6 lines
utils/cec-follower/cec-tuner.cpp +6 -0
+	tuner_dev_info.analog.ana_freq = (ana_freq_khz * 10) / 625;
+}
+
+static bool set_analog_tuner_dev_info(const struct cec_msg *msg)
+{
+	tuner_dev_info.rec_flag = CEC_OP_REC_FLAG_NOT_USED;

utils/cec-follower/cec-tuner.cpp static unsigned int analog_freqs_khz[3][9][3] = · line 108

Quoted 10 lines
utils/cec-follower/cec-tuner.cpp +10 -0
+	tuner_dev_info.tuner_display_info = CEC_OP_TUNER_DISPLAY_INFO_ANALOGUE;
+	tuner_dev_info.is_analog = true;
+	cec_ops_select_analogue_service(msg,
+					&tuner_dev_info.analog.ana_bcast_type,
+					&tuner_dev_info.analog.ana_freq,
+					&tuner_dev_info.analog.bcast_system);
+	if (tuner_dev_info.analog.ana_bcast_type > 2 ||
+	    tuner_dev_info.analog.bcast_system > 8)
+		return false;
+	set_analog_channel_freq(analog_freqs_khz[1][tuner_dev_info.analog.bcast_system][tuner_dev_info.analog.ana_bcast_type]);

Shouldn't this be

set_analog_channel_freq(analog_freqs_khz[tuner_dev_info.analog.bcast_type][tuner_dev_info.analog.ana_bcast_type][1]);

Rather than using the middle frequency, find the frequency that's closest to the requested frequency.

Update tuner_dev_info.analog.ana_freq with that new frequency so that this is stored in the state.

Quoted 20 lines
utils/cec-follower/cec-tuner.cpp +4 -6
+	return true;
+}
+
 void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, unsigned me)
 {
 	switch (msg.msg[1]) {
@@ -101,29 +123,34 @@ void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns
 		*/
 
 	case CEC_MSG_GIVE_TUNER_DEVICE_STATUS: {
-		if (!cec_has_tuner(1 << me))
+		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
 			break;
 
-		struct cec_op_tuner_device_info tuner_dev_info = {};
-
 		cec_msg_set_reply_to(&msg, &msg);
-		tuner_dev_info.rec_flag = CEC_OP_REC_FLAG_NOT_USED;
-		tuner_dev_info.tuner_display_info = CEC_OP_TUNER_DISPLAY_INFO_NONE;
-		tuner_dev_info.is_analog = false;

utils/cec-follower/cec-tuner.cpp void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns · line 122

Quoted 10 lines
utils/cec-follower/cec-tuner.cpp +0 -6
-		tuner_dev_info.digital.service_id_method = CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL;
-		tuner_dev_info.digital.dig_bcast_system = CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_C;
-		tuner_dev_info.digital.channel.channel_number_fmt = CEC_OP_CHANNEL_NUMBER_FMT_1_PART;
-		tuner_dev_info.digital.channel.minor = 1;
-
 		cec_msg_tuner_device_status(&msg, &tuner_dev_info);
 		transmit(node, &msg);
 		return;
 	}
-

Spurious line deletion.

utils/cec-follower/cec-tuner.cpp void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns · line 144

Quoted 11 lines
utils/cec-follower/cec-tuner.cpp +7 -0
 	case CEC_MSG_TUNER_DEVICE_STATUS:
 		return;
 
 	case CEC_MSG_SELECT_ANALOGUE_SERVICE:
+		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
+			break;
+
+		if (tuner_dev_info.rec_flag == CEC_OP_REC_FLAG_USED) {
+			msg.msg[1] = CEC_MSG_FEATURE_ABORT;
+			msg.msg[2] = CEC_OP_ABORT_REFUSED;
+			transmit(node, &msg);

Use the reply_feature_abort() function from cec-processing.cpp for this.

utils/cec-follower/cec-tuner.cpp void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns · line 153

Quoted 9 lines
utils/cec-follower/cec-tuner.cpp +9 -0
+			return;
+		}
+		if (!set_analog_tuner_dev_info(&msg)) {
+			msg.msg[1] = CEC_MSG_FEATURE_ABORT;
+			msg.msg[2] = CEC_OP_ABORT_INVALID_OP;
+			transmit(node, &msg);
+			return;
+		}
+		return;

Add a newline here.

utils/cec-follower/cec-tuner.cpp void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns · line 156

Quoted 4 lines
utils/cec-follower/cec-tuner.cpp +0 -0
 	case CEC_MSG_SELECT_DIGITAL_SERVICE:
 	case CEC_MSG_TUNER_STEP_DECREMENT:
 	case CEC_MSG_TUNER_STEP_INCREMENT:

Regards,

Hans

#2 lore raw

PATCH v2 1/1 cec-follower: add tuner analog service emulation

In reply to c0d1n61at3 at gmail.com (Jiunn Chang)

Implement the following tuner control features:
- <Select Analogue Service>
- <Give Tuner Device Status> and reply <Tuner Device Status>

Signed-off-by Jiunn Chang <c0d1n61at3 at gmail.com>
---
 utils/cec-follower/cec-follower.h |  1 +
 utils/cec-follower/cec-tuner.cpp  | 89 ++++++++++++++++++++++++++-----
 2 files changed, 78 insertions(+), 12 deletions(-)
utils/cec-follower/cec-follower.h +1 -0
index 9f5f1be4..9c146be1 100644
@@ -51,6 +51,7 @@ struct state {
 	__u64 rc_press_rx_ts;
 	unsigned rc_press_hold_count;
 	unsigned rc_duration_sum;
+	struct cec_op_tuner_device_info tuner_dev_info;
 };
 
 struct node {
utils/cec-follower/cec-tuner.cpp +77 -12

116 lines not on this page · raw message

Signature · 1 line
2.23.0
#3 lore raw

PATCH v3 1/1 cec-follower: add tuner analog service emulation

Implement the following tuner control features:
- <Select Analogue Service>
- <Give Tuner Device Status> and reply <Tuner Device Status>

Signed-off-by Jiunn Chang <c0d1n61at3 at gmail.com>

---

Changes made since v1:
- Fix typos/bugs
- Import reply_feature_abort() from cec-processing.cpp
- Add functionality to choose nearest frequency

Changes made since v2:
- Fix typos/bugs
- Use state from node in cec-follower.h
- Rename functions to analog_ prefix

---
 utils/cec-follower/cec-follower.h |  1 +
 utils/cec-follower/cec-tuner.cpp  | 87 ++++++++++++++++++++++++++-----
 2 files changed, 76 insertions(+), 12 deletions(-)
utils/cec-follower/cec-follower.h +1 -0
index 9f5f1be4..9c146be1 100644
@@ -51,6 +51,7 @@ struct state {
 	__u64 rc_press_rx_ts;
 	unsigned rc_press_hold_count;
 	unsigned rc_duration_sum;
+	struct cec_op_tuner_device_info tuner_dev_info;
 };
 
 struct node {
utils/cec-follower/cec-tuner.cpp +75 -12

114 lines not on this page · raw message

Signature · 1 line
2.23.0
#4 lore raw

PATCH v4 1/1 cec-follower: add tuner analog service emulation

Implement the following tuner control features:
- <Select Analogue Service>
- <Give Tuner Device Status> and reply <Tuner Device Status>

Signed-off-by Jiunn Chang <c0d1n61at3 at gmail.com>

---

Changes made since v1:
- Fix typos/bugs
- Import reply_feature_abort() from cec-processing.cpp
- Add functionality to choose nearest frequency

Changes made since v2:
- Fix typos/bugs
- Use state from node in cec-follower.h
- Rename functions to analog_ prefix

Changes made since v3:
- Refactor lines numbers since a fix patch was needed for cec-tuner.cpp

---
 utils/cec-follower/cec-follower.h |  1 +
 utils/cec-follower/cec-tuner.cpp  | 57 ++++++++++++++++++++++++-------
 2 files changed, 46 insertions(+), 12 deletions(-)
utils/cec-follower/cec-follower.h +1 -0
index 9f5f1be4..9c146be1 100644
@@ -51,6 +51,7 @@ struct state {
 	__u64 rc_press_rx_ts;
 	unsigned rc_press_hold_count;
 	unsigned rc_duration_sum;
+	struct cec_op_tuner_device_info tuner_dev_info;
 };
 
 struct node {
utils/cec-follower/cec-tuner.cpp +45 -12

84 lines not on this page · raw message

Signature · 1 line
2.23.0
#5 lore raw

PATCH v4 1/1 cec-follower: add tuner analog service emulation

Quoted 50 lines · Jiunn Chang

Implement the following tuner control features:
- <Select Analogue Service>
- <Give Tuner Device Status> and reply <Tuner Device Status>

Signed-off-by Jiunn Chang <c0d1n61at3 at gmail.com>

---

Changes made since v1:
- Fix typos/bugs
- Import reply_feature_abort() from cec-processing.cpp
- Add functionality to choose nearest frequency

Changes made since v2:
- Fix typos/bugs
- Use state from node in cec-follower.h
- Rename functions to analog_ prefix

Changes made since v3:
- Refactor lines numbers since a fix patch was needed for cec-tuner.cpp

---
 utils/cec-follower/cec-follower.h |  1 +
 utils/cec-follower/cec-tuner.cpp  | 57 ++++++++++++++++++++++++-------
 2 files changed, 46 insertions(+), 12 deletions(-)
utils/cec-follower/cec-follower.h +1 -0
index 9f5f1be4..9c146be1 100644
@@ -51,6 +51,7 @@ struct state {
 	__u64 rc_press_rx_ts;
 	unsigned rc_press_hold_count;
 	unsigned rc_duration_sum;
+	struct cec_op_tuner_device_info tuner_dev_info;
 };
 
 struct node {
utils/cec-follower/cec-tuner.cpp +1 -0
index 2303e6bb..87c631e4 100644
@@ -4,6 +4,7 @@
  */
 
 #include <sys/ioctl.h>
+#include <stdlib.h>
 
 #include "cec-follower.h"
 

utils/cec-follower/cec-tuner.cpp static void reply_feature_abort(struct node *node, struct cec_msg *msg, __u8 rea · line 124

Quoted 7 lines
utils/cec-follower/cec-tuner.cpp +4 -0
@@ -117,6 +118,35 @@ static void reply_feature_abort(struct node *node, struct cec_msg *msg, __u8 rea
 	transmit(node, msg);
 }
 
+static void analog_set_tuner_chan_freq(struct node *node)
+{
+	unsigned int ana_freq_khz = (node->state.tuner_dev_info.analog.ana_freq * 625) / 10;
+	unsigned int nearest = analog_freqs_khz[node->state.tuner_dev_info.analog.ana_bcast_type][node->state.tuner_dev_info.analog.bcast_system][0];

I'd make both variable 'int', since that will avoid the int cast in the abs function.

Add newline after variable declarations.

Also make a temp variable like this:

	struct cec_op_tuner_device_info *info = &node->state.tuner_dev_info;

and use that instead instead of repeating 'node->state.tuner_dev_info.' everywhere. That's way too long.

utils/cec-follower/cec-tuner.cpp static void reply_feature_abort(struct node *node, struct cec_msg *msg, __u8 rea · line 125

Quoted 1 line
utils/cec-follower/cec-tuner.cpp +1 -0
+	for (int i = 0; i < 3; i++) {

Rather than hardcoding '3' here, add a NUM_ANALOG_FREQS define and use that.

Of course, the analog_freqs_khz array declaration should use this new define as well. This means a new version of 'cec-follower: create analog channel frequencies'.

I'd add a:

		int freq = analog_freqs_khz[info->analog.ana_bcast_type][info->analog.bcast_system][i];

and use that below.

utils/cec-follower/cec-tuner.cpp static void reply_feature_abort(struct node *node, struct cec_msg *msg, __u8 rea · line 138

Quoted 13 lines
utils/cec-follower/cec-tuner.cpp +13 -0
+		if (abs(int(ana_freq_khz - analog_freqs_khz[node->state.tuner_dev_info.analog.ana_bcast_type][node->state.tuner_dev_info.analog.bcast_system][i])) <
+		    abs(int(ana_freq_khz - nearest))) {
+			nearest = analog_freqs_khz[node->state.tuner_dev_info.analog.ana_bcast_type][node->state.tuner_dev_info.analog.bcast_system][i];
+		}
+	}
+	node->state.tuner_dev_info.analog.ana_freq = (nearest * 10) / 625;
+}
+
+static bool analog_set_tuner_dev_info(struct node *node, struct cec_msg *msg)
+{
+	node->state.tuner_dev_info.rec_flag = CEC_OP_REC_FLAG_NOT_USED;
+	node->state.tuner_dev_info.tuner_display_info = CEC_OP_TUNER_DISPLAY_INFO_ANALOGUE;
+	node->state.tuner_dev_info.is_analog = true;

Same here, use info pointer.

Quoted 49 lines
utils/cec-follower/cec-tuner.cpp +19 -12
+	cec_ops_select_analogue_service(msg,
+					&node->state.tuner_dev_info.analog.ana_bcast_type,
+					&node->state.tuner_dev_info.analog.ana_freq,
+					&node->state.tuner_dev_info.analog.bcast_system);
+	if (node->state.tuner_dev_info.analog.ana_bcast_type > 2 ||
+	    node->state.tuner_dev_info.analog.bcast_system > 8)
+		return false;
+	analog_set_tuner_chan_freq(node);
+	return true;
+}
+
 void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, unsigned me)
 {
 	bool is_bcast = cec_msg_is_broadcast(&msg);
@@ -133,21 +163,11 @@ void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns
 		*/
 
 	case CEC_MSG_GIVE_TUNER_DEVICE_STATUS: {
-		if (!cec_has_tuner(1 << me))
+		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
 			break;
 
-		struct cec_op_tuner_device_info tuner_dev_info = {};
-
 		cec_msg_set_reply_to(&msg, &msg);
-		tuner_dev_info.rec_flag = CEC_OP_REC_FLAG_NOT_USED;
-		tuner_dev_info.tuner_display_info = CEC_OP_TUNER_DISPLAY_INFO_NONE;
-		tuner_dev_info.is_analog = false;
-		tuner_dev_info.digital.service_id_method = CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL;
-		tuner_dev_info.digital.dig_bcast_system = CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_C;
-		tuner_dev_info.digital.channel.channel_number_fmt = CEC_OP_CHANNEL_NUMBER_FMT_1_PART;
-		tuner_dev_info.digital.channel.minor = 1;
-
-		cec_msg_tuner_device_status(&msg, &tuner_dev_info);
+		cec_msg_tuner_device_status(&msg, &node->state.tuner_dev_info);
 		transmit(node, &msg);
 		return;
 	}
@@ -156,6 +176,19 @@ void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns
 		return;
 
 	case CEC_MSG_SELECT_ANALOGUE_SERVICE:
+		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
+			break;
+
+		if (node->state.tuner_dev_info.rec_flag == CEC_OP_REC_FLAG_USED) {
+			reply_feature_abort(node, &msg, CEC_OP_ABORT_REFUSED);
+			return;

utils/cec-follower/cec-tuner.cpp void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns · line 194

Quoted 10 lines
utils/cec-follower/cec-tuner.cpp +7 -0
+		}
+		if (!analog_set_tuner_dev_info(node, &msg)) {
+			reply_feature_abort(node, &msg, CEC_OP_ABORT_INVALID_OP);
+			return;
+		}
+		return;
+
 	case CEC_MSG_SELECT_DIGITAL_SERVICE:
 	case CEC_MSG_TUNER_STEP_DECREMENT:
 	case CEC_MSG_TUNER_STEP_INCREMENT:

Can you post a patch series next time? There are now three patches on top of one another, it's easier to review if I see the whole set.

Regards,

Hans

#6 lore raw

PATCH v5 1/1 cec-follower: add tuner analog service emulation

In reply to c0d1n61at3 at gmail.com (Jiunn Chang)

Implement the following tuner control features:
- <Select Analogue Service>
- <Give Tuner Device Status> and reply <Tuner Device Status>

Signed-off-by Jiunn Chang <c0d1n61at3 at gmail.com>

---

Changes made since v1:
- Fix typos/bugs
- Import reply_feature_abort() from cec-processing.cpp
- Add functionality to choose nearest frequency

Changes made since v2:
- Fix typos/bugs
- Use state from node in cec-follower.h
- Rename functions to analog_ prefix

Changes made since v3:
- Refactor lines numbers since a fix patch was needed for cec-tuner.cpp

Changes made since v4:
- Remove int casting in abs()
- Add temp variables: info, freq to make code easier to read
- Use NUM_ANALOG_FREQS macro

---
 utils/cec-follower/cec-follower.h |  1 +
 utils/cec-follower/cec-tuner.cpp  | 62 +++++++++++++++++++++++++------
 2 files changed, 51 insertions(+), 12 deletions(-)
utils/cec-follower/cec-follower.h +1 -0
index 03b52217..91b9e037 100644
@@ -51,6 +51,7 @@ struct state {
 	__u64 rc_press_rx_ts;
 	unsigned rc_press_hold_count;
 	unsigned rc_duration_sum;
+	struct cec_op_tuner_device_info tuner_dev_info;
 };
 
 struct node {
utils/cec-follower/cec-tuner.cpp +50 -12

89 lines not on this page · raw message

Signature · 1 line
2.23.0
#7 lore raw

PATCH v5 1/1 cec-follower: add tuner analog service emulation

Hi Jiunn,

This is a lot better. Just a few more comments:

Quoted 60 lines · Jiunn Chang

Implement the following tuner control features:
- <Select Analogue Service>
- <Give Tuner Device Status> and reply <Tuner Device Status>

Signed-off-by Jiunn Chang <c0d1n61at3 at gmail.com>

---

Changes made since v1:
- Fix typos/bugs
- Import reply_feature_abort() from cec-processing.cpp
- Add functionality to choose nearest frequency

Changes made since v2:
- Fix typos/bugs
- Use state from node in cec-follower.h
- Rename functions to analog_ prefix

Changes made since v3:
- Refactor lines numbers since a fix patch was needed for cec-tuner.cpp

Changes made since v4:
- Remove int casting in abs()
- Add temp variables: info, freq to make code easier to read
- Use NUM_ANALOG_FREQS macro

---
 utils/cec-follower/cec-follower.h |  1 +
 utils/cec-follower/cec-tuner.cpp  | 62 +++++++++++++++++++++++++------
 2 files changed, 51 insertions(+), 12 deletions(-)
utils/cec-follower/cec-follower.h +1 -0
index 03b52217..91b9e037 100644
@@ -51,6 +51,7 @@ struct state {
 	__u64 rc_press_rx_ts;
 	unsigned rc_press_hold_count;
 	unsigned rc_duration_sum;
+	struct cec_op_tuner_device_info tuner_dev_info;
 };
 
 struct node {
utils/cec-follower/cec-tuner.cpp +3 -0
index bdeda342..87425083 100644
@@ -4,6 +4,7 @@
  */
 
 #include <sys/ioctl.h>
+#include <stdlib.h>
 
 #include "cec-follower.h"
 
@@ -89,6 +90,40 @@ static unsigned int analog_freqs_khz[3][9][NUM_ANALOG_FREQS] =
 	}
 };
 
+static void analog_set_tuner_chan_freq(struct node *node)
+{

utils/cec-follower/cec-tuner.cpp static unsigned int analog_freqs_khz[3][9][NUM_ANALOG_FREQS] = · line 104

Quoted 10 lines
utils/cec-follower/cec-tuner.cpp +10 -0
+	struct cec_op_tuner_device_info *info = &node->state.tuner_dev_info;
+	int ana_freq_khz = (info->analog.ana_freq * 625) / 10;
+	int nearest = analog_freqs_khz[info->analog.ana_bcast_type][info->analog.bcast_system][0];
+
+	for (int i = 0; i < NUM_ANALOG_FREQS; i++) {
+		int freq = analog_freqs_khz[info->analog.ana_bcast_type][info->analog.bcast_system][i];
+
+		if (abs(ana_freq_khz - freq) < abs(ana_freq_khz - nearest)) {
+			nearest = freq;
+		}

You can drop the { } as per the kernel coding style.

Quoted 8 lines
utils/cec-follower/cec-tuner.cpp +8 -0
+	}
+	info->analog.ana_freq = (nearest * 10) / 625;
+}
+
+static bool analog_set_tuner_dev_info(struct node *node, struct cec_msg *msg)
+{
+	struct cec_op_tuner_device_info *info = &node->state.tuner_dev_info;
+

utils/cec-follower/cec-tuner.cpp static unsigned int analog_freqs_khz[3][9][NUM_ANALOG_FREQS] = · line 122

Quoted 10 lines
utils/cec-follower/cec-tuner.cpp +10 -0
+	info->rec_flag = CEC_OP_REC_FLAG_NOT_USED;
+	info->tuner_display_info = CEC_OP_TUNER_DISPLAY_INFO_ANALOGUE;
+	info->is_analog = true;
+	cec_ops_select_analogue_service(msg,
+					&info->analog.ana_bcast_type,
+					&info->analog.ana_freq,
+					&info->analog.bcast_system);
+	if (info->analog.ana_bcast_type > 2 ||
+	    info->analog.bcast_system > 8)
+		return false;

cec_ops_select_analogue_service should store the results in temp variables first and this function should only change node->state.tuner_dev_info if the test above succeeds. Otherwise you would change the internal state to invalid values.

I also think that analog_set_tuner_chan_freq() shouldn't set info->analog.ana_freq. That really should happen in this function as that keeps the code that updates node->state.tuner_dev_info in a single place. Instead that function should just find the nearest valid frequency, so the prototype becomes something like this:

static unsigned int analog_get_nearest_freq(__u8 ana_bcast_type, __u8 ana_bcast_system,

					    int ana_freq_khz);

This will be needed for e.g. timer/record support as well.

Quoted 42 lines
utils/cec-follower/cec-tuner.cpp +12 -12
+	analog_set_tuner_chan_freq(node);
+	return true;
+}
+
 void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, unsigned me)
 {
 	bool is_bcast = cec_msg_is_broadcast(&msg);
@@ -105,21 +140,11 @@ void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns
 		*/
 
 	case CEC_MSG_GIVE_TUNER_DEVICE_STATUS: {
-		if (!cec_has_tuner(1 << me))
+		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
 			break;
 
-		struct cec_op_tuner_device_info tuner_dev_info = {};
-
 		cec_msg_set_reply_to(&msg, &msg);
-		tuner_dev_info.rec_flag = CEC_OP_REC_FLAG_NOT_USED;
-		tuner_dev_info.tuner_display_info = CEC_OP_TUNER_DISPLAY_INFO_NONE;
-		tuner_dev_info.is_analog = false;
-		tuner_dev_info.digital.service_id_method = CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL;
-		tuner_dev_info.digital.dig_bcast_system = CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_C;
-		tuner_dev_info.digital.channel.channel_number_fmt = CEC_OP_CHANNEL_NUMBER_FMT_1_PART;
-		tuner_dev_info.digital.channel.minor = 1;
-
-		cec_msg_tuner_device_status(&msg, &tuner_dev_info);
+		cec_msg_tuner_device_status(&msg, &node->state.tuner_dev_info);
 		transmit(node, &msg);
 		return;
 	}
@@ -128,6 +153,19 @@ void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns
 		return;
 
 	case CEC_MSG_SELECT_ANALOGUE_SERVICE:
+		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
+			break;
+
+		if (node->state.tuner_dev_info.rec_flag == CEC_OP_REC_FLAG_USED) {
+			reply_feature_abort(node, &msg, CEC_OP_ABORT_REFUSED);
+			return;

utils/cec-follower/cec-tuner.cpp void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns · line 171

Quoted 10 lines
utils/cec-follower/cec-tuner.cpp +7 -0
+		}
+		if (!analog_set_tuner_dev_info(node, &msg)) {
+			reply_feature_abort(node, &msg, CEC_OP_ABORT_INVALID_OP);
+			return;
+		}
+		return;
+
 	case CEC_MSG_SELECT_DIGITAL_SERVICE:
 	case CEC_MSG_TUNER_STEP_DECREMENT:
 	case CEC_MSG_TUNER_STEP_INCREMENT:

I think there is one more thing to do here: state_init() in cec-follower.cpp should initialize node->state.tuner_dev_info with sane initial values.

It is probably best to have it call a analog_tuner_init() function in cec-tuner.cpp since it will need to access the analog_freqs_khz table.

Regards,

Hans

#8 lore raw

PATCH v6 1/1 cec-follower: add tuner analog service emulation

In reply to c0d1n61at3 at gmail.com (Jiunn Chang)

Implement the following tuner control features:
- <Select Analogue Service>
- <Give Tuner Device Status> and reply <Tuner Device Status>

Signed-off-by Jiunn Chang <c0d1n61at3 at gmail.com>

---

Changes made since v1:
- Fix typos/bugs
- Import reply_feature_abort() from cec-processing.cpp
- Add functionality to choose nearest frequency

Changes made since v2:
- Fix typos/bugs
- Use state from node in cec-follower.h
- Rename functions to analog_ prefix

Changes made since v3:
- Refactor lines numbers since a fix patch was needed for cec-tuner.cpp

Changes made since v4:
- Remove int casting in abs()
- Add temp variables: info, freq to make code easier to read
- Use NUM_ANALOG_FREQS macro

Changes made since v5:
- Add analog_tuner_init() to cec-tuner.cpp
- Change state_init() in cec-follower.cpp to use analog_tuner_init()
- Rename function to analog_get_nearest_freq()
- Change analog_get_nearest_freq() to only return freq in Khz
- Change analog_set_tuner_dev_info() to set analog freq too

---
 utils/cec-follower/cec-follower.cpp |  1 +
 utils/cec-follower/cec-follower.h   |  2 +
 utils/cec-follower/cec-tuner.cpp    | 73 ++++++++++++++++++++++++-----
 3 files changed, 64 insertions(+), 12 deletions(-)
utils/cec-follower/cec-follower.cpp +1 -0
index dca0f627..4243fdd9 100644
@@ -298,6 +298,7 @@ void state_init(struct node &node)
 	node.state.sac_active = false;
 	node.state.volume = 50;
 	node.state.mute = false;
+	analog_tuner_init(&node.state.tuner_dev_info);
 }
 
 int main(int argc, char **argv)
utils/cec-follower/cec-follower.h +2 -0
index 954e001f..a53c16fe 100644
@@ -51,6 +51,7 @@ struct state {
 	__u64 rc_press_rx_ts;
 	unsigned rc_press_hold_count;
 	unsigned rc_duration_sum;
+	struct cec_op_tuner_device_info tuner_dev_info;
 };
 
 struct node {
@@ -220,6 +221,7 @@ std::string opcode2s(const struct cec_msg *msg);
 void sad_encode(const struct short_audio_desc *sad, __u32 *descriptor);
 
 // cec-tuner.cpp
+void analog_tuner_init(struct cec_op_tuner_device_info *tuner_dev_info);
 void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, unsigned me);
 
 // CEC processing
utils/cec-follower/cec-tuner.cpp +61 -12

100 lines not on this page · raw message

Signature · 1 line
2.23.0
#9 lore raw

PATCH v6 1/1 cec-follower: add tuner analog service emulation

Quoted 4 lines · Jiunn Chang

Implement the following tuner control features:
- <Select Analogue Service>
- <Give Tuner Device Status> and reply <Tuner Device Status>

More information is needed on what this feature does. This commit log to terse.

Quoted 21 lines
Signed-off-by Jiunn Chang <c0d1n61at3 at gmail.com>

---

Changes made since v1:
- Fix typos/bugs
- Import reply_feature_abort() from cec-processing.cpp
- Add functionality to choose nearest frequency

Changes made since v2:
- Fix typos/bugs
- Use state from node in cec-follower.h
- Rename functions to analog_ prefix

Changes made since v3:
- Refactor lines numbers since a fix patch was needed for cec-tuner.cpp

Changes made since v4:
- Remove int casting in abs()
- Add temp variables: info, freq to make code easier to read
- Use NUM_ANALOG_FREQS macro

Quoted 7 lines

Changes made since v5:
- Add analog_tuner_init() to cec-tuner.cpp
- Change state_init() in cec-follower.cpp to use analog_tuner_init()
- Rename function to analog_get_nearest_freq()
- Change analog_get_nearest_freq() to only return freq in Khz
- Change analog_set_tuner_dev_info() to set analog freq too

I think it is better to reverse this list, so your recent version is at the top.

Quoted 147 lines
---
  utils/cec-follower/cec-follower.cpp |  1 +
  utils/cec-follower/cec-follower.h   |  2 +
  utils/cec-follower/cec-tuner.cpp    | 73 ++++++++++++++++++++++++-----
  3 files changed, 64 insertions(+), 12 deletions(-)
utils/cec-follower/cec-follower.cpp +1 -0
index dca0f627..4243fdd9 100644
@@ -298,6 +298,7 @@ void state_init(struct node &node)
  	node.state.sac_active = false;
  	node.state.volume = 50;
  	node.state.mute = false;
+	analog_tuner_init(&node.state.tuner_dev_info);
  }
 
  int main(int argc, char **argv)
utils/cec-follower/cec-follower.h +2 -0
index 954e001f..a53c16fe 100644
@@ -51,6 +51,7 @@ struct state {
  	__u64 rc_press_rx_ts;
  	unsigned rc_press_hold_count;
  	unsigned rc_duration_sum;
+	struct cec_op_tuner_device_info tuner_dev_info;
  };
 
  struct node {
@@ -220,6 +221,7 @@ std::string opcode2s(const struct cec_msg *msg);
  void sad_encode(const struct short_audio_desc *sad, __u32 *descriptor);
 
  // cec-tuner.cpp
+void analog_tuner_init(struct cec_op_tuner_device_info *tuner_dev_info);
  void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, unsigned me);
 
  // CEC processing
utils/cec-follower/cec-tuner.cpp +61 -12
index bdeda342..68d945d5 100644
@@ -4,6 +4,7 @@
   */
 
  #include <sys/ioctl.h>
+#include <stdlib.h>
 
  #include "cec-follower.h"
 
@@ -89,6 +90,51 @@ static unsigned int analog_freqs_khz[3][9][NUM_ANALOG_FREQS] =
  	}
  };
 
+void analog_tuner_init(struct cec_op_tuner_device_info *info)
+{
+	info->rec_flag = CEC_OP_REC_FLAG_NOT_USED;
+	info->tuner_display_info = CEC_OP_TUNER_DISPLAY_INFO_ANALOGUE;
+	info->is_analog = true;
+	info->analog.ana_bcast_type = CEC_OP_ANA_BCAST_TYPE_CABLE;
+	info->analog.ana_freq =
+		analog_freqs_khz[CEC_OP_ANA_BCAST_TYPE_CABLE][CEC_OP_BCAST_SYSTEM_PAL_BG][0];
+	info->analog.bcast_system = CEC_OP_BCAST_SYSTEM_PAL_BG;
+}
+
+static unsigned int analog_get_nearest_freq(__u8 ana_bcast_type, __u8 ana_bcast_system,
+                                            int ana_freq_khz)
+{
+	int nearest = analog_freqs_khz[ana_bcast_type][ana_bcast_system][0];
+
+	for (int i = 0; i < NUM_ANALOG_FREQS; i++) {
+		int freq = analog_freqs_khz[ana_bcast_type][ana_bcast_system][i];
+
+		if (abs(ana_freq_khz - freq) < abs(ana_freq_khz - nearest))
+			nearest = freq;
+	}
+	return nearest;
+}
+
+static bool analog_set_tuner_dev_info(struct node *node, struct cec_msg *msg)
+{
+	struct cec_op_tuner_device_info *info = &node->state.tuner_dev_info;
+	__u8 type = 0;
+	__u16 freq = 0;
+	__u8 system = 0;
+
+	cec_ops_select_analogue_service(msg, &type, &freq, &system);
+	if (info->analog.ana_bcast_type < 3 && info->analog.bcast_system < 9) {
+		int freq_khz = (freq * 625) / 10;
+		unsigned int nearest = analog_get_nearest_freq(type, system,
+							       freq_khz);
+		info->analog.ana_bcast_type = type;
+		info->analog.ana_freq = (nearest * 10) / 625;
+		info->analog.bcast_system = system;
+		return true;
+	}
+	return false;
+}
+
  void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, unsigned me)
  {
  	bool is_bcast = cec_msg_is_broadcast(&msg);
@@ -105,21 +151,11 @@ void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns
  		*/
 
  	case CEC_MSG_GIVE_TUNER_DEVICE_STATUS: {
-		if (!cec_has_tuner(1 << me))
+		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
  			break;
 
-		struct cec_op_tuner_device_info tuner_dev_info = {};
-
  		cec_msg_set_reply_to(&msg, &msg);
-		tuner_dev_info.rec_flag = CEC_OP_REC_FLAG_NOT_USED;
-		tuner_dev_info.tuner_display_info = CEC_OP_TUNER_DISPLAY_INFO_NONE;
-		tuner_dev_info.is_analog = false;
-		tuner_dev_info.digital.service_id_method = CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL;
-		tuner_dev_info.digital.dig_bcast_system = CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_C;
-		tuner_dev_info.digital.channel.channel_number_fmt = CEC_OP_CHANNEL_NUMBER_FMT_1_PART;
-		tuner_dev_info.digital.channel.minor = 1;
-
-		cec_msg_tuner_device_status(&msg, &tuner_dev_info);
+		cec_msg_tuner_device_status(&msg, &node->state.tuner_dev_info);
  		transmit(node, &msg);
  		return;
  	}
@@ -128,6 +164,19 @@ void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns
  		return;
 
  	case CEC_MSG_SELECT_ANALOGUE_SERVICE:
+		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
+			break;
+
+		if (node->state.tuner_dev_info.rec_flag == CEC_OP_REC_FLAG_USED) {
+			reply_feature_abort(node, &msg, CEC_OP_ABORT_REFUSED);
+			return;
+		}
+		if (!analog_set_tuner_dev_info(node, &msg)) {
+			reply_feature_abort(node, &msg, CEC_OP_ABORT_INVALID_OP);
+			return;
+		}
+		return;
+
  	case CEC_MSG_SELECT_DIGITAL_SERVICE:
  	case CEC_MSG_TUNER_STEP_DECREMENT:
  	case CEC_MSG_TUNER_STEP_INCREMENT:
#10 lore raw

PATCH v7 1/1 cec-follower: add tuner analog service emulation

In reply to Jiunn Chang

The cec-follower will now emulate an analog service. This allows an initiator device can directly select an analog service by choosing an analog broadcast type, broadcast system, and frequency. After an analog service is selected, the cec-follower will also provide the tuner device status upon request.

Opcodes implemented:
- <Select Analogue Service>
- <Give Tuner Device Status>

Signed-off-by Jiunn Chang <c0d1n61at3 at gmail.com>

---

Changes made since v6:
- Add more information to commit message

Changes made since v5:
- Add analog_tuner_init() to cec-tuner.cpp
- Change state_init() in cec-follower.cpp to use analog_tuner_init()
- Rename function to analog_get_nearest_freq()
- Change analog_get_nearest_freq() to only return freq in Khz
- Change analog_set_tuner_dev_info() to set analog freq too

Changes made since v4:
- Remove int casting in abs()
- Add temp variables: info, freq to make code easier to read
- Use NUM_ANALOG_FREQS macro

Changes made since v3:
- Refactor lines numbers since a fix patch was needed for cec-tuner.cpp

Changes made since v2:
- Fix typos/bugs
- Use state from node in cec-follower.h
- Rename functions to analog_ prefix

Changes made since v1:
- Fix typos/bugs
- Import reply_feature_abort() from cec-processing.cpp
- Add functionality to choose nearest frequency

---
 utils/cec-follower/cec-follower.cpp |  1 +
 utils/cec-follower/cec-follower.h   |  2 +
 utils/cec-follower/cec-tuner.cpp    | 73 ++++++++++++++++++++++++-----
 3 files changed, 64 insertions(+), 12 deletions(-)
utils/cec-follower/cec-follower.cpp +1 -0
index dca0f627..4243fdd9 100644
@@ -298,6 +298,7 @@ void state_init(struct node &node)
 	node.state.sac_active = false;
 	node.state.volume = 50;
 	node.state.mute = false;
+	analog_tuner_init(&node.state.tuner_dev_info);
 }
 
 int main(int argc, char **argv)
utils/cec-follower/cec-follower.h +2 -0
index 954e001f..a53c16fe 100644
@@ -51,6 +51,7 @@ struct state {
 	__u64 rc_press_rx_ts;
 	unsigned rc_press_hold_count;
 	unsigned rc_duration_sum;
+	struct cec_op_tuner_device_info tuner_dev_info;
 };
 
 struct node {
@@ -220,6 +221,7 @@ std::string opcode2s(const struct cec_msg *msg);
 void sad_encode(const struct short_audio_desc *sad, __u32 *descriptor);
 
 // cec-tuner.cpp
+void analog_tuner_init(struct cec_op_tuner_device_info *tuner_dev_info);
 void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, unsigned me);
 
 // CEC processing
utils/cec-follower/cec-tuner.cpp +61 -12

100 lines not on this page · raw message

Signature · 1 line
2.23.0
#11 lore raw

PATCH v7 1/1 cec-follower: add tuner analog service emulation

Quoted 94 lines · Jiunn Chang

The cec-follower will now emulate an analog service. This allows an initiator device can directly select an analog service by choosing an analog broadcast type, broadcast system, and frequency. After an analog service is selected, the cec-follower will also provide the tuner device status upon request.

Opcodes implemented:
- <Select Analogue Service>
- <Give Tuner Device Status>

Signed-off-by Jiunn Chang <c0d1n61at3 at gmail.com>

---

Changes made since v6:
- Add more information to commit message

Changes made since v5:
- Add analog_tuner_init() to cec-tuner.cpp
- Change state_init() in cec-follower.cpp to use analog_tuner_init()
- Rename function to analog_get_nearest_freq()
- Change analog_get_nearest_freq() to only return freq in Khz
- Change analog_set_tuner_dev_info() to set analog freq too

Changes made since v4:
- Remove int casting in abs()
- Add temp variables: info, freq to make code easier to read
- Use NUM_ANALOG_FREQS macro

Changes made since v3:
- Refactor lines numbers since a fix patch was needed for cec-tuner.cpp

Changes made since v2:
- Fix typos/bugs
- Use state from node in cec-follower.h
- Rename functions to analog_ prefix

Changes made since v1:
- Fix typos/bugs
- Import reply_feature_abort() from cec-processing.cpp
- Add functionality to choose nearest frequency

---
 utils/cec-follower/cec-follower.cpp |  1 +
 utils/cec-follower/cec-follower.h   |  2 +
 utils/cec-follower/cec-tuner.cpp    | 73 ++++++++++++++++++++++++-----
 3 files changed, 64 insertions(+), 12 deletions(-)
utils/cec-follower/cec-follower.cpp +1 -0
index dca0f627..4243fdd9 100644
@@ -298,6 +298,7 @@ void state_init(struct node &node)
 	node.state.sac_active = false;
 	node.state.volume = 50;
 	node.state.mute = false;
+	analog_tuner_init(&node.state.tuner_dev_info);
 }
 
 int main(int argc, char **argv)
utils/cec-follower/cec-follower.h +2 -0
index 954e001f..a53c16fe 100644
@@ -51,6 +51,7 @@ struct state {
 	__u64 rc_press_rx_ts;
 	unsigned rc_press_hold_count;
 	unsigned rc_duration_sum;
+	struct cec_op_tuner_device_info tuner_dev_info;
 };
 
 struct node {
@@ -220,6 +221,7 @@ std::string opcode2s(const struct cec_msg *msg);
 void sad_encode(const struct short_audio_desc *sad, __u32 *descriptor);
 
 // cec-tuner.cpp
+void analog_tuner_init(struct cec_op_tuner_device_info *tuner_dev_info);
 void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, unsigned me);
 
 // CEC processing
utils/cec-follower/cec-tuner.cpp +1 -0
index bdeda342..68d945d5 100644
@@ -4,6 +4,7 @@
  */
 
 #include <sys/ioctl.h>
+#include <stdlib.h>
 
 #include "cec-follower.h"
 
@@ -89,6 +90,51 @@ static unsigned int analog_freqs_khz[3][9][NUM_ANALOG_FREQS] =
 	}
 };

utils/cec-follower/cec-tuner.cpp static unsigned int analog_freqs_khz[3][9][NUM_ANALOG_FREQS] = · line 101

Quoted 10 lines
utils/cec-follower/cec-tuner.cpp +9 -0
 
+void analog_tuner_init(struct cec_op_tuner_device_info *info)
+{
+	info->rec_flag = CEC_OP_REC_FLAG_NOT_USED;
+	info->tuner_display_info = CEC_OP_TUNER_DISPLAY_INFO_ANALOGUE;
+	info->is_analog = true;
+	info->analog.ana_bcast_type = CEC_OP_ANA_BCAST_TYPE_CABLE;
+	info->analog.ana_freq =
+		analog_freqs_khz[CEC_OP_ANA_BCAST_TYPE_CABLE][CEC_OP_BCAST_SYSTEM_PAL_BG][0];
+	info->analog.bcast_system = CEC_OP_BCAST_SYSTEM_PAL_BG;

Set bcast_system before setting ana_freq, and when you set ana_freq use the analog.ana_bcast_type and analog.bcast_system as indices.

That way you avoid the case where in the future someone changes e.g. ana_bcast_type but forgets to update the index in the ana_freq assignment as well.

It's a little thing, but it makes for more robust programs.

Quoted 12 lines
utils/cec-follower/cec-tuner.cpp +12 -0
+}
+
+static unsigned int analog_get_nearest_freq(__u8 ana_bcast_type, __u8 ana_bcast_system,
+                                            int ana_freq_khz)
+{
+	int nearest = analog_freqs_khz[ana_bcast_type][ana_bcast_system][0];
+
+	for (int i = 0; i < NUM_ANALOG_FREQS; i++) {
+		int freq = analog_freqs_khz[ana_bcast_type][ana_bcast_system][i];
+
+		if (abs(ana_freq_khz - freq) < abs(ana_freq_khz - nearest))
+			nearest = freq;

utils/cec-follower/cec-tuner.cpp static unsigned int analog_freqs_khz[3][9][NUM_ANALOG_FREQS] = · line 123

Quoted 10 lines
utils/cec-follower/cec-tuner.cpp +10 -0
+	}
+	return nearest;
+}
+
+static bool analog_set_tuner_dev_info(struct node *node, struct cec_msg *msg)
+{
+	struct cec_op_tuner_device_info *info = &node->state.tuner_dev_info;
+	__u8 type = 0;
+	__u16 freq = 0;
+	__u8 system = 0;

No need to zero these three variables. cec_ops_select_analogue_service will always overwrite them.

utils/cec-follower/cec-tuner.cpp static unsigned int analog_freqs_khz[3][9][NUM_ANALOG_FREQS] = · line 129

Quoted 6 lines
utils/cec-follower/cec-tuner.cpp +6 -0
+
+	cec_ops_select_analogue_service(msg, &type, &freq, &system);
+	if (info->analog.ana_bcast_type < 3 && info->analog.bcast_system < 9) {
+		int freq_khz = (freq * 625) / 10;
+		unsigned int nearest = analog_get_nearest_freq(type, system,
+							       freq_khz);

Add empty line between variable declarations and the following code.

Quoted 46 lines
utils/cec-follower/cec-tuner.cpp +16 -12
+		info->analog.ana_bcast_type = type;
+		info->analog.ana_freq = (nearest * 10) / 625;
+		info->analog.bcast_system = system;
+		return true;
+	}
+	return false;
+}
+
 void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, unsigned me)
 {
 	bool is_bcast = cec_msg_is_broadcast(&msg);
@@ -105,21 +151,11 @@ void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns
 		*/
 
 	case CEC_MSG_GIVE_TUNER_DEVICE_STATUS: {
-		if (!cec_has_tuner(1 << me))
+		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
 			break;
 
-		struct cec_op_tuner_device_info tuner_dev_info = {};
-
 		cec_msg_set_reply_to(&msg, &msg);
-		tuner_dev_info.rec_flag = CEC_OP_REC_FLAG_NOT_USED;
-		tuner_dev_info.tuner_display_info = CEC_OP_TUNER_DISPLAY_INFO_NONE;
-		tuner_dev_info.is_analog = false;
-		tuner_dev_info.digital.service_id_method = CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL;
-		tuner_dev_info.digital.dig_bcast_system = CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_C;
-		tuner_dev_info.digital.channel.channel_number_fmt = CEC_OP_CHANNEL_NUMBER_FMT_1_PART;
-		tuner_dev_info.digital.channel.minor = 1;
-
-		cec_msg_tuner_device_status(&msg, &tuner_dev_info);
+		cec_msg_tuner_device_status(&msg, &node->state.tuner_dev_info);
 		transmit(node, &msg);
 		return;
 	}
@@ -128,6 +164,19 @@ void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns
 		return;
 
 	case CEC_MSG_SELECT_ANALOGUE_SERVICE:
+		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
+			break;
+
+		if (node->state.tuner_dev_info.rec_flag == CEC_OP_REC_FLAG_USED) {
+			reply_feature_abort(node, &msg, CEC_OP_ABORT_REFUSED);
+			return;

utils/cec-follower/cec-tuner.cpp void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns · line 182

Quoted 10 lines
utils/cec-follower/cec-tuner.cpp +7 -0
+		}
+		if (!analog_set_tuner_dev_info(node, &msg)) {
+			reply_feature_abort(node, &msg, CEC_OP_ABORT_INVALID_OP);
+			return;
+		}
+		return;
+
 	case CEC_MSG_SELECT_DIGITAL_SERVICE:
 	case CEC_MSG_TUNER_STEP_DECREMENT:
 	case CEC_MSG_TUNER_STEP_INCREMENT:

Regards,

Hans

#12 lore raw

PATCH v8 1/1 cec-follower: add tuner analog service emulation

In reply to c0d1n61at3 at gmail.com (Jiunn Chang)

The cec-follower will now emulate an analog service. This allows an initiator device can directly select an analog service by choosing an analog broadcast type, broadcast system, and frequency. After an analog service is selected, the cec-follower will also provide the tuner device status upon request.

Opcodes implemented:
- <Select Analogue Service>
- <Give Tuner Device Status>

Signed-off-by Jiunn Chang <c0d1n61at3 at gmail.com>

---

Changes made since v7:
- Make analog_tuner_init() more robust
- Remove redundancy from analog_set_tuner_dev_info()

Changes made since v6:
- Add more information to commit message

Changes made since v5:
- Add analog_tuner_init() to cec-tuner.cpp
- Change state_init() in cec-follower.cpp to use analog_tuner_init()
- Rename function to analog_get_nearest_freq()
- Change analog_get_nearest_freq() to only return freq in Khz
- Change analog_set_tuner_dev_info() to set analog freq too

Changes made since v4:
- Remove int casting in abs()
- Add temp variables: info, freq to make code easier to read
- Use NUM_ANALOG_FREQS macro

Changes made since v3:
- Refactor lines numbers since a fix patch was needed for cec-tuner.cpp

Changes made since v2:
- Fix typos/bugs
- Use state from node in cec-follower.h
- Rename functions to analog_ prefix

Changes made since v1:
- Fix typos/bugs
- Import reply_feature_abort() from cec-processing.cpp
- Add functionality to choose nearest frequency

---
 utils/cec-follower/cec-follower.cpp |  1 +
 utils/cec-follower/cec-follower.h   |  2 +
 utils/cec-follower/cec-tuner.cpp    | 73 ++++++++++++++++++++++++-----
 3 files changed, 64 insertions(+), 12 deletions(-)
utils/cec-follower/cec-follower.cpp +1 -0
index dca0f627..4243fdd9 100644
@@ -298,6 +298,7 @@ void state_init(struct node &node)
 	node.state.sac_active = false;
 	node.state.volume = 50;
 	node.state.mute = false;
+	analog_tuner_init(&node.state.tuner_dev_info);
 }
 
 int main(int argc, char **argv)
utils/cec-follower/cec-follower.h +2 -0
index 954e001f..a53c16fe 100644
@@ -51,6 +51,7 @@ struct state {
 	__u64 rc_press_rx_ts;
 	unsigned rc_press_hold_count;
 	unsigned rc_duration_sum;
+	struct cec_op_tuner_device_info tuner_dev_info;
 };
 
 struct node {
@@ -220,6 +221,7 @@ std::string opcode2s(const struct cec_msg *msg);
 void sad_encode(const struct short_audio_desc *sad, __u32 *descriptor);
 
 // cec-tuner.cpp
+void analog_tuner_init(struct cec_op_tuner_device_info *tuner_dev_info);
 void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, unsigned me);
 
 // CEC processing
utils/cec-follower/cec-tuner.cpp +61 -12

100 lines not on this page · raw message

Signature · 1 line
2.23.0
#13 lore raw

PATCH v8 1/1 cec-follower: add tuner analog service emulation

Quoted 2 lines · Jiunn Chang

The cec-follower will now emulate an analog service. This allows an initiator device can directly select an analog service by choosing an

to select? Something doesn't read right in this sentence.

Quoted 94 lines

analog broadcast type, broadcast system, and frequency. After an analog service is selected, the cec-follower will also provide the tuner device status upon request.

Opcodes implemented:
- <Select Analogue Service>
- <Give Tuner Device Status>

Signed-off-by Jiunn Chang <c0d1n61at3 at gmail.com>

---

Changes made since v7:
- Make analog_tuner_init() more robust
- Remove redundancy from analog_set_tuner_dev_info()

Changes made since v6:
- Add more information to commit message

Changes made since v5:
- Add analog_tuner_init() to cec-tuner.cpp
- Change state_init() in cec-follower.cpp to use analog_tuner_init()
- Rename function to analog_get_nearest_freq()
- Change analog_get_nearest_freq() to only return freq in Khz
- Change analog_set_tuner_dev_info() to set analog freq too

Changes made since v4:
- Remove int casting in abs()
- Add temp variables: info, freq to make code easier to read
- Use NUM_ANALOG_FREQS macro

Changes made since v3:
- Refactor lines numbers since a fix patch was needed for cec-tuner.cpp

Changes made since v2:
- Fix typos/bugs
- Use state from node in cec-follower.h
- Rename functions to analog_ prefix

Changes made since v1:
- Fix typos/bugs
- Import reply_feature_abort() from cec-processing.cpp
- Add functionality to choose nearest frequency

---
  utils/cec-follower/cec-follower.cpp |  1 +
  utils/cec-follower/cec-follower.h   |  2 +
  utils/cec-follower/cec-tuner.cpp    | 73 ++++++++++++++++++++++++-----
  3 files changed, 64 insertions(+), 12 deletions(-)
utils/cec-follower/cec-follower.cpp +1 -0
index dca0f627..4243fdd9 100644
@@ -298,6 +298,7 @@ void state_init(struct node &node)
  	node.state.sac_active = false;
  	node.state.volume = 50;
  	node.state.mute = false;
+	analog_tuner_init(&node.state.tuner_dev_info);
  }
 
  int main(int argc, char **argv)
utils/cec-follower/cec-follower.h +2 -0
index 954e001f..a53c16fe 100644
@@ -51,6 +51,7 @@ struct state {
  	__u64 rc_press_rx_ts;
  	unsigned rc_press_hold_count;
  	unsigned rc_duration_sum;
+	struct cec_op_tuner_device_info tuner_dev_info;
  };
 
  struct node {
@@ -220,6 +221,7 @@ std::string opcode2s(const struct cec_msg *msg);
  void sad_encode(const struct short_audio_desc *sad, __u32 *descriptor);
 
  // cec-tuner.cpp
+void analog_tuner_init(struct cec_op_tuner_device_info *tuner_dev_info);
  void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, unsigned me);
 
  // CEC processing
utils/cec-follower/cec-tuner.cpp +1 -0
index bdeda342..acc3fd00 100644
@@ -4,6 +4,7 @@
   */
 
  #include <sys/ioctl.h>
+#include <stdlib.h>
 
  #include "cec-follower.h"
 

utils/cec-follower/cec-tuner.cpp static unsigned int analog_freqs_khz[3][9][NUM_ANALOG_FREQS] = · line 99

Quoted 10 lines
utils/cec-follower/cec-tuner.cpp +7 -0
@@ -89,6 +90,51 @@ static unsigned int analog_freqs_khz[3][9][NUM_ANALOG_FREQS] =
  	}
  };
 
+void analog_tuner_init(struct cec_op_tuner_device_info *info)
+{
+	info->rec_flag = CEC_OP_REC_FLAG_NOT_USED;
+	info->tuner_display_info = CEC_OP_TUNER_DISPLAY_INFO_ANALOGUE;
+	info->is_analog = true;
+	info->analog.ana_bcast_type = CEC_OP_ANA_BCAST_TYPE_CABLE;
+	info->analog.bcast_system = CEC_OP_BCAST_SYSTEM_PAL_BG;

Does this need to support NTSC as well? Can user select one or the other?

Quoted 76 lines
utils/cec-follower/cec-tuner.cpp +46 -12
+	info->analog.ana_freq =
+		analog_freqs_khz[info->analog.ana_bcast_type][info->analog.bcast_system][0];
+}
+
+static unsigned int analog_get_nearest_freq(__u8 ana_bcast_type, __u8 ana_bcast_system,
+                                            int ana_freq_khz)
+{
+	int nearest = analog_freqs_khz[ana_bcast_type][ana_bcast_system][0];
+
+	for (int i = 0; i < NUM_ANALOG_FREQS; i++) {
+		int freq = analog_freqs_khz[ana_bcast_type][ana_bcast_system][i];
+
+		if (abs(ana_freq_khz - freq) < abs(ana_freq_khz - nearest))
+			nearest = freq;
+	}
+	return nearest;
+}
+
+static bool analog_set_tuner_dev_info(struct node *node, struct cec_msg *msg)
+{
+	struct cec_op_tuner_device_info *info = &node->state.tuner_dev_info;
+	__u8 type;
+	__u16 freq;
+	__u8 system;
+
+	cec_ops_select_analogue_service(msg, &type, &freq, &system);
+	if (info->analog.ana_bcast_type < 3 && info->analog.bcast_system < 9) {
+		int freq_khz = (freq * 625) / 10;
+		unsigned int nearest = analog_get_nearest_freq(type, system,
+							       freq_khz);
+		info->analog.ana_bcast_type = type;
+		info->analog.ana_freq = (nearest * 10) / 625;
+		info->analog.bcast_system = system;
+		return true;
+	}
+	return false;
+}
+
  void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, unsigned me)
  {
  	bool is_bcast = cec_msg_is_broadcast(&msg);
@@ -105,21 +151,11 @@ void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns
  		*/
 
  	case CEC_MSG_GIVE_TUNER_DEVICE_STATUS: {
-		if (!cec_has_tuner(1 << me))
+		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
  			break;
 
-		struct cec_op_tuner_device_info tuner_dev_info = {};
-
  		cec_msg_set_reply_to(&msg, &msg);
-		tuner_dev_info.rec_flag = CEC_OP_REC_FLAG_NOT_USED;
-		tuner_dev_info.tuner_display_info = CEC_OP_TUNER_DISPLAY_INFO_NONE;
-		tuner_dev_info.is_analog = false;
-		tuner_dev_info.digital.service_id_method = CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL;
-		tuner_dev_info.digital.dig_bcast_system = CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_C;
-		tuner_dev_info.digital.channel.channel_number_fmt = CEC_OP_CHANNEL_NUMBER_FMT_1_PART;
-		tuner_dev_info.digital.channel.minor = 1;
-
-		cec_msg_tuner_device_status(&msg, &tuner_dev_info);
+		cec_msg_tuner_device_status(&msg, &node->state.tuner_dev_info);
  		transmit(node, &msg);
  		return;
  	}
@@ -128,6 +164,19 @@ void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns
  		return;
 
  	case CEC_MSG_SELECT_ANALOGUE_SERVICE:
+		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
+			break;
+
+		if (node->state.tuner_dev_info.rec_flag == CEC_OP_REC_FLAG_USED) {
+			reply_feature_abort(node, &msg, CEC_OP_ABORT_REFUSED);
+			return;

utils/cec-follower/cec-tuner.cpp void process_tuner_record_timer_msgs(struct node *node, struct cec_msg &msg, uns · line 182

Quoted 10 lines
utils/cec-follower/cec-tuner.cpp +7 -0
+		}
+		if (!analog_set_tuner_dev_info(node, &msg)) {
+			reply_feature_abort(node, &msg, CEC_OP_ABORT_INVALID_OP);
+			return;
+		}
+		return;
+
  	case CEC_MSG_SELECT_DIGITAL_SERVICE:
  	case CEC_MSG_TUNER_STEP_DECREMENT:
  	case CEC_MSG_TUNER_STEP_INCREMENT:

thanks,
-- Shuah