On 3/5/20 11:20 AM, Gabriel Krisman Bertazi wrote:
Bart Van Assche <bvanassche@xxxxxxx> writes:
On 3/5/20 7:35 AM, Gabriel Krisman Bertazi wrote:
+static const struct {
+ int value;
+ char *name;
+} connection_state_names[] = {
+ {ISCSI_CONN_UP, "up"},
+ {ISCSI_CONN_DOWN, "down"},
+ {ISCSI_CONN_FAILED, "failed"}
+};
+
+static const char *connection_state_name(int state)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(connection_state_names); i++) {
+ if (connection_state_names[i].value == state)
+ return connection_state_names[i].name;
+ }
+ return NULL;
+}
+
+static ssize_t show_conn_state(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent);
+
+ return sprintf(buf, "%s\n", connection_state_name(conn->state));
+}
+static ISCSI_CLASS_ATTR(conn, state, S_IRUGO, show_conn_state,
+ NULL);
What has been changed in v2 compared to v1? Please always include a
changelog when posting a new version.
Additionally, it seems like the comment I posted on v1 has not been
addressed?
Hi Bart,
v2 no longer has the dependency for specific values for the state, as
you requested. It follows the pattern in similar places in the iscsi
code. Why would designated initializers be better than my solution?
Hi Gabriel,
How about removing the loop as follows?
static const char *const connection_state_names[] = {
[ISCSI_CONN_UP] = "up",
[ISCSI_CONN_DOWN] = "down",
[ISCSI_CONN_FAILED] = "failed",
};
...
if ((unsigned)conn->state < ARRAY_SIZE(connection_state_names))
return sprintf(buf, "%s\n",
connection_state_names[conn->state]);
else
...
...
Thanks,
Bart.