Skip to contentSkip to conversation

sim: change pin_retries array to unsigned char

PATCH In review Cedric Jehasse 2 participants 4 messages Nov 28, 2014 – Dec 4, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sim: change pin_retries array to unsigned char

From: Cedric Jehasse <[email protected]>

Had a problem on mips, the Retries dict in org.ofono.SimManager was 0 for all pin-types.
The problem is int* are passed to dbus-functions, while the type is DBUS_TYPE_BYTE. Changed the type of pin_retries throughout the code to match the byte type.

---
 drivers/atmodem/sim.c         | 24 ++++++++++++------------
 drivers/isimodem/uicc.c       |  2 +-
 drivers/qmimodem/sim-legacy.c |  2 +-
 drivers/qmimodem/sim.c        |  2 +-
 include/sim.h                 |  2 +-
 src/sim.c                     | 13 +++++++------
 6 files changed, 23 insertions(+), 22 deletions(-)
drivers/atmodem/sim.c +12 -12
index c128e33..0f64602 100644
@@ -479,7 +479,7 @@ static struct {
 
 #define BUILD_PIN_RETRIES_ARRAY(passwd_types, passwd_types_cnt, retry)	\
 	for (i = 0; i < OFONO_SIM_PASSWORD_INVALID; i++)		\
-		retry[i] = -1;						\
+		retry[i] = 0xff;					\
 									\
 	for (i = 0; i < passwd_types_cnt; i++) {			\
 		int val;						\
@@ -500,7 +500,7 @@ static void huawei_cpin_cb(gboolean ok, GAtResult *result, gpointer user_data)
 	const char *final = g_at_result_final_response(result);
 	GAtResultIter iter;
 	struct ofono_error error;
-	int retries[OFONO_SIM_PASSWORD_INVALID];
+	unsigned char retries[OFONO_SIM_PASSWORD_INVALID];
 	size_t i;
 	static enum ofono_sim_password_type password_types[] = {
 		OFONO_SIM_PASSWORD_SIM_PUK,
@@ -547,7 +547,7 @@ static void pinnum_cb(gboolean ok, GAtResult *result, gpointer user_data)
 	const char *final = g_at_result_final_response(result);
 	GAtResultIter iter;
 	struct ofono_error error;
-	int retries[OFONO_SIM_PASSWORD_INVALID];
+	unsigned char retries[OFONO_SIM_PASSWORD_INVALID];
 	size_t i;
 	static enum ofono_sim_password_type password_types[] = {
 		OFONO_SIM_PASSWORD_SIM_PIN,
@@ -587,7 +587,7 @@ static void zpinpuk_cb(gboolean ok, GAtResult *result, gpointer user_data)
 	const char *final = g_at_result_final_response(result);
 	GAtResultIter iter;
 	struct ofono_error error;
-	int retries[OFONO_SIM_PASSWORD_INVALID];
+	unsigned char retries[OFONO_SIM_PASSWORD_INVALID];
 	size_t i;
 	static enum ofono_sim_password_type password_types[] = {
 		OFONO_SIM_PASSWORD_SIM_PIN,
@@ -625,7 +625,7 @@ static void xpincnt_cb(gboolean ok, GAtResult *result, gpointer user_data)
 	const char *final = g_at_result_final_response(result);
 	GAtResultIter iter;
 	struct ofono_error error;
-	int retries[OFONO_SIM_PASSWORD_INVALID];
+	unsigned char retries[OFONO_SIM_PASSWORD_INVALID];
 	size_t i;
 	static enum ofono_sim_password_type password_types[] = {
 		OFONO_SIM_PASSWORD_SIM_PIN,
@@ -664,7 +664,7 @@ static void oercn_cb(gboolean ok, GAtResult *result, gpointer user_data)
 	const char *final = g_at_result_final_response(result);
 	GAtResultIter iter;
 	struct ofono_error error;
-	int retries[OFONO_SIM_PASSWORD_INVALID];
+	unsigned char retries[OFONO_SIM_PASSWORD_INVALID];
 	size_t i;
 	static enum ofono_sim_password_type password_types[] = {
 		OFONO_SIM_PASSWORD_SIM_PIN,
@@ -704,7 +704,7 @@ static void cpnnum_cb(gboolean ok, GAtResult *result, gpointer user_data)
 	const char *line;
 	int num;
 	char **entries;
-	int retries[OFONO_SIM_PASSWORD_INVALID];
+	unsigned char retries[OFONO_SIM_PASSWORD_INVALID];
 	size_t i;
 
 	decode_at_error(&error, final);
@@ -760,7 +760,7 @@ static void at_epin_cb(gboolean ok, GAtResult *result, gpointer user_data)
 	const char *final = g_at_result_final_response(result);
 	GAtResultIter iter;
 	struct ofono_error error;
-	int retries[OFONO_SIM_PASSWORD_INVALID];
+	unsigned char retries[OFONO_SIM_PASSWORD_INVALID];
 	size_t i;
 	static enum ofono_sim_password_type password_types[] = {
 		OFONO_SIM_PASSWORD_SIM_PIN,
@@ -798,7 +798,7 @@ static void at_cpinr_cb(gboolean ok, GAtResult *result, gpointer user_data)
 	ofono_sim_pin_retries_cb_t cb = cbd->cb;
 	GAtResultIter iter;
 	struct ofono_error error;
-	int retries[OFONO_SIM_PASSWORD_INVALID];
+	unsigned char retries[OFONO_SIM_PASSWORD_INVALID];
 	size_t len = sizeof(at_sim_name) / sizeof(*at_sim_name);
 	size_t i;
 
@@ -843,7 +843,7 @@ static void at_spic_cb(gboolean ok, GAtResult *result, gpointer user_data)
 	const char *final = g_at_result_final_response(result);
 	GAtResultIter iter;
 	struct ofono_error error;
-	int retries[OFONO_SIM_PASSWORD_INVALID];
+	unsigned char retries[OFONO_SIM_PASSWORD_INVALID];
 	size_t i;
 	static enum ofono_sim_password_type password_types[] = {
 		OFONO_SIM_PASSWORD_SIM_PIN,
@@ -887,7 +887,7 @@ static void at_pct_cb(gboolean ok, GAtResult *result, gpointer user_data)
 	const char *final = g_at_result_final_response(result);
 	GAtResultIter iter;
 	struct ofono_error error;
-	int retries[OFONO_SIM_PASSWORD_INVALID];
+	unsigned char retries[OFONO_SIM_PASSWORD_INVALID];
 	size_t i;
 	enum ofono_sim_password_type pin_type;
 
@@ -939,7 +939,7 @@ static void at_pnnm_cb(gboolean ok, GAtResult *result, gpointer user_data)
 	const char *final = g_at_result_final_response(result);
 	GAtResultIter iter;
 	struct ofono_error error;
-	int retries[OFONO_SIM_PASSWORD_INVALID];
+	unsigned char retries[OFONO_SIM_PASSWORD_INVALID];
 	size_t i;
 	static enum ofono_sim_password_type password_types[] = {
 		OFONO_SIM_PASSWORD_SIM_PIN,
drivers/isimodem/uicc.c +1 -1
index 0a5121d..a075d27 100644
@@ -926,7 +926,7 @@ static void uicc_query_pin_retries_resp(const GIsiMessage *msg, void *opaque)
 {
 	struct isi_cb_data *cbd = opaque;
 	ofono_sim_pin_retries_cb_t cb = cbd->cb;
-	int retries[OFONO_SIM_PASSWORD_INVALID];
+	unsigned char retries[OFONO_SIM_PASSWORD_INVALID];
 	GIsiSubBlockIter iter;
 
 	uint8_t num_sb = 0;
drivers/qmimodem/sim-legacy.c +1 -1
index 318b1ae..5b1bb5e 100644
@@ -38,7 +38,7 @@
 
 struct sim_data {
 	struct qmi_service *dms;
-	int retries[OFONO_SIM_PASSWORD_INVALID];
+	unsigned char retries[OFONO_SIM_PASSWORD_INVALID];
 };
 
 static void qmi_read_file_info(struct ofono_sim *sim, int fileid,
drivers/qmimodem/sim.c +1 -1
index 197da50..f7ccd06 100644
@@ -44,7 +44,7 @@ struct sim_data {
 	uint8_t card_state;
 	uint8_t app_type;
 	uint8_t passwd_state;
-	int retries[OFONO_SIM_PASSWORD_INVALID];
+	unsigned char retries[OFONO_SIM_PASSWORD_INVALID];
 };
 
 static int create_fileid_data(uint8_t app_type, int fileid,
include/sim.h +1 -1
index ed850f9..f66bb93 100644
@@ -113,7 +113,7 @@ typedef void (*ofono_sim_passwd_cb_t)(const struct ofono_error *error,
 					void *data);
 
 typedef void (*ofono_sim_pin_retries_cb_t)(const struct ofono_error *error,
-			int retries[OFONO_SIM_PASSWORD_INVALID], void *data);
+			unsigned char retries[OFONO_SIM_PASSWORD_INVALID], void *data);
 
 typedef void (*ofono_sim_lock_unlock_cb_t)(const struct ofono_error *error,
 					void *data);
src/sim.c +7 -6
index edae5eb..fe871a0 100644
@@ -62,7 +62,7 @@ struct ofono_sim {
 	enum ofono_sim_password_type pin_type;
 	gboolean locked_pins[OFONO_SIM_PASSWORD_SIM_PUK]; /* Number of PINs */
 
-	int pin_retries[OFONO_SIM_PASSWORD_INVALID];
+	unsigned char pin_retries[OFONO_SIM_PASSWORD_INVALID];
 
 	enum ofono_sim_phase phase;
 	unsigned char mnc_length;
@@ -273,7 +273,7 @@ static void **get_pin_retries(struct ofono_sim *sim)
 	void **ret;
 
 	for (i = 1, nelem = 0; i < OFONO_SIM_PASSWORD_INVALID; i++) {
-		if (sim->pin_retries[i] == -1)
+		if (sim->pin_retries[i] == 0xff)
 			continue;
 
 		nelem += 1;
@@ -282,7 +282,7 @@ static void **get_pin_retries(struct ofono_sim *sim)
 	ret = g_new0(void *, nelem * 2 + 1);
 
 	for (i = 1, nelem = 0; i < OFONO_SIM_PASSWORD_INVALID; i++) {
-		if (sim->pin_retries[i] == -1)
+		if (sim->pin_retries[i] == 0xff)
 			continue;
 
 		ret[nelem++] = (void *) sim_passwd_name(i);
@@ -431,7 +431,7 @@ done:
 }
 
 static void sim_pin_retries_query_cb(const struct ofono_error *error,
-					int retries[OFONO_SIM_PASSWORD_INVALID],
+					unsigned char retries[OFONO_SIM_PASSWORD_INVALID],
 					void *data)
 {
 	struct ofono_sim *sim = data;
@@ -450,6 +450,7 @@ static void sim_pin_retries_query_cb(const struct ofono_error *error,
 	memcpy(sim->pin_retries, retries, sizeof(sim->pin_retries));
 
 	pin_retries = get_pin_retries(sim);
+
 	ofono_dbus_signal_dict_property_changed(conn, path,
 					OFONO_SIM_MANAGER_INTERFACE, "Retries",
 					DBUS_TYPE_BYTE,	&pin_retries);
@@ -2372,7 +2373,7 @@ static void sim_free_main_state(struct ofono_sim *sim)
 	int i;
 
 	for (i = 0; i < OFONO_SIM_PASSWORD_INVALID; i++)
-		sim->pin_retries[i] = -1;
+		sim->pin_retries[i] = 0xff;
 
 	memset(sim->locked_pins, 0, sizeof(sim->locked_pins));
 
@@ -2881,7 +2882,7 @@ struct ofono_sim *ofono_sim_create(struct ofono_modem *modem,
 						sim_remove, sim);
 
 	for (i = 0; i < OFONO_SIM_PASSWORD_INVALID; i++)
-		sim->pin_retries[i] = -1;
+		sim->pin_retries[i] = 0xff;
 
 	for (l = g_drivers; l; l = l->next) {
 		const struct ofono_sim_driver *drv = l->data;
Signature · 1 line
1.9.2
#1 lore raw

Hi Cedric,

Quoted 7 lines · Cedric Jehasse

From: Cedric Jehasse <[email protected]>

Had a problem on mips, the Retries dict in org.ofono.SimManager was 0 for all pin-types.
The problem is int* are passed to dbus-functions, while the type is DBUS_TYPE_BYTE. Changed the type of pin_retries throughout the code to match the byte type.

Quoted 9 lines
---
  drivers/atmodem/sim.c         | 24 ++++++++++++------------
  drivers/isimodem/uicc.c       |  2 +-
  drivers/qmimodem/sim-legacy.c |  2 +-
  drivers/qmimodem/sim.c        |  2 +-
  include/sim.h                 |  2 +-
  src/sim.c                     | 13 +++++++------
  6 files changed, 23 insertions(+), 22 deletions(-)

I really rather not modify every single driver for this issue. Besides, the actual problem is actually in the core. We were being quite evil with how we created our dictionary entries.

Please try the following patch (attached) and tell me if it fixes this bug on your platform.

Regards,
-Denis

Quoted 1 line

From 7359b0322d93f6a332000c1064258d299c533bc3 Mon Sep 17 00:00:00 2001

From: Denis Kenzior <[email protected]>
Date: Wed, 3 Dec 2014 17:31:09 -0600
Subject: [PATCH] sim: Fix pin retries bogus values on some arch-es

On some architectures the SimManager.Retries property was getting bogus values. This is because we were sending an array which pointed to int values instead of the expected unsigned char values.

This fix allocates a temporary array of unsigned chars to hold the actual D-Bus values being sent. Additionally, the dictionary array is changed to point to the temporary unsigned char based values instead of the raw 'int' based retry values.

---
 src/sim.c | 40 +++++++++++++++++++++++++---------------
 1 file changed, 25 insertions(+), 15 deletions(-)
src/sim.c +25 -15
index edae5eb..6e9baa9 100644
@@ -267,10 +267,12 @@ static char **get_locked_pins(struct ofono_sim *sim)
 	return ret;
 }
 
-static void **get_pin_retries(struct ofono_sim *sim)
+static void get_pin_retries(struct ofono_sim *sim, void ***out_dict,
+				unsigned char **out_retries)
 {
 	int i, nelem;
-	void **ret;
+	void **dict;
+	unsigned char *retries;
 
 	for (i = 1, nelem = 0; i < OFONO_SIM_PASSWORD_INVALID; i++) {
 		if (sim->pin_retries[i] == -1)
@@ -279,17 +281,21 @@ static void **get_pin_retries(struct ofono_sim *sim)
 		nelem += 1;
 	}
 
-	ret = g_new0(void *, nelem * 2 + 1);
+	dict = g_new0(void *, nelem * 2 + 1);
+	retries = g_new0(unsigned char, nelem);
 
-	for (i = 1, nelem = 0; i < OFONO_SIM_PASSWORD_INVALID; i++) {
+	for (i = 1, nelem = 0; i < OFONO_SIM_PASSWORD_INVALID; i++, nelem++) {
 		if (sim->pin_retries[i] == -1)
 			continue;
 
-		ret[nelem++] = (void *) sim_passwd_name(i);
-		ret[nelem++] = &sim->pin_retries[i];
+		retries[nelem] = sim->pin_retries[i];
+
+		dict[nelem * 2] = (void *) sim_passwd_name(i);
+		dict[nelem * 2 + 1] = &retries[i];
 	}
 
-	return ret;
+	*out_dict = dict;
+	*out_retries = retries;
 }
 
 static char **get_service_numbers(GSList *service_numbers)
@@ -344,7 +350,8 @@ static DBusMessage *sim_get_properties(DBusConnection *conn,
 	char **service_numbers;
 	char **locked_pins;
 	const char *pin_name;
-	void **pin_retries;
+	void **pin_retries_dict;
+	unsigned char *dbus_retries;
 	dbus_bool_t present = sim->state != OFONO_SIM_STATE_NOT_PRESENT;
 	dbus_bool_t fdn;
 	dbus_bool_t bdn;
@@ -419,10 +426,11 @@ static DBusMessage *sim_get_properties(DBusConnection *conn,
 				DBUS_TYPE_STRING,
 				(void *) &pin_name);
 
-	pin_retries = get_pin_retries(sim);
+	get_pin_retries(sim, &pin_retries_dict, &dbus_retries);
 	ofono_dbus_dict_append_dict(&dict, "Retries", DBUS_TYPE_BYTE,
-								&pin_retries);
-	g_free(pin_retries);
+							&pin_retries_dict);
+	g_free(pin_retries_dict);
+	g_free(dbus_retries);
 
 done:
 	dbus_message_iter_close_container(&iter, &dict);
@@ -437,7 +445,8 @@ static void sim_pin_retries_query_cb(const struct ofono_error *error,
 	struct ofono_sim *sim = data;
 	DBusConnection *conn = ofono_dbus_get_connection();
 	const char *path = __ofono_atom_get_path(sim->atom);
-	void **pin_retries;
+	void **pin_retries_dict;
+	unsigned char *dbus_retries;
 
 	if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
 		ofono_error("Querying remaining pin retries failed");
@@ -449,11 +458,12 @@ static void sim_pin_retries_query_cb(const struct ofono_error *error,
 
 	memcpy(sim->pin_retries, retries, sizeof(sim->pin_retries));
 
-	pin_retries = get_pin_retries(sim);
+	get_pin_retries(sim, &pin_retries_dict, &dbus_retries);
 	ofono_dbus_signal_dict_property_changed(conn, path,
 					OFONO_SIM_MANAGER_INTERFACE, "Retries",
-					DBUS_TYPE_BYTE,	&pin_retries);
-	g_free(pin_retries);
+					DBUS_TYPE_BYTE,	&pin_retries_dict);
+	g_free(pin_retries_dict);
+	g_free(dbus_retries);
 }
 
 static void sim_pin_retries_check(struct ofono_sim *sim)
Signature · 1 line
2.0.4
#2 lore raw

Hi Denis,

Quoted 2 lines

Please try the following patch (attached) and tell me if it fixes this bug on your platform.

This patch caused a segfault. Please have a look at the reworked patch (attached).

Thanks,
Cedric

From d69bf227fe736392b532992d57e58e7e6bb08f36 Mon Sep 17 00:00:00 2001
From: Denis Kenzior <[email protected]>
Date: Wed, 3 Dec 2014 17:31:09 -0600
Subject: [PATCH] sim: Fix pin retries bogus values on some arch-es

On some architectures the SimManager.Retries property was getting bogus values. This is because we were sending an array which pointed to int values instead of the expected unsigned char values.

This fix allocates a temporary array of unsigned chars to hold the actual D-Bus values being sent. Additionally, the dictionary array is changed to point to the temporary unsigned char based values instead of the raw 'int' based retry values.

---
 src/sim.c | 39 +++++++++++++++++++++++++--------------
 1 file changed, 25 insertions(+), 14 deletions(-)
src/sim.c +25 -14
index edae5eb..82f7409 100644
@@ -267,10 +267,12 @@ static char **get_locked_pins(struct ofono_sim *sim)
 	return ret;
 }
 
-static void **get_pin_retries(struct ofono_sim *sim)
+static void get_pin_retries(struct ofono_sim *sim, void ***out_dict,
+				unsigned char **out_retries)
 {
 	int i, nelem;
-	void **ret;
+	void **dict;
+	unsigned char *retries;
 
 	for (i = 1, nelem = 0; i < OFONO_SIM_PASSWORD_INVALID; i++) {
 		if (sim->pin_retries[i] == -1)
@@ -279,17 +281,22 @@ static void **get_pin_retries(struct ofono_sim *sim)
 		nelem += 1;
 	}
 
-	ret = g_new0(void *, nelem * 2 + 1);
+	dict = g_new0(void *, nelem * 2 + 1);
+	retries = g_new0(unsigned char, nelem);
 
 	for (i = 1, nelem = 0; i < OFONO_SIM_PASSWORD_INVALID; i++) {
 		if (sim->pin_retries[i] == -1)
 			continue;
 
-		ret[nelem++] = (void *) sim_passwd_name(i);
-		ret[nelem++] = &sim->pin_retries[i];
+		retries[nelem] = sim->pin_retries[i];
+
+		dict[nelem * 2] = (void *) sim_passwd_name(i);
+		dict[nelem * 2 + 1] = &retries[nelem];
+		nelem++;
 	}
 
-	return ret;
+	*out_dict = dict;
+	*out_retries = retries;
 }
 
 static char **get_service_numbers(GSList *service_numbers)
@@ -344,7 +351,8 @@ static DBusMessage *sim_get_properties(DBusConnection *conn,
 	char **service_numbers;
 	char **locked_pins;
 	const char *pin_name;
-	void **pin_retries;
+	void **pin_retries_dict;
+	unsigned char *dbus_retries;
 	dbus_bool_t present = sim->state != OFONO_SIM_STATE_NOT_PRESENT;
 	dbus_bool_t fdn;
 	dbus_bool_t bdn;
@@ -419,10 +427,11 @@ static DBusMessage *sim_get_properties(DBusConnection *conn,
 				DBUS_TYPE_STRING,
 				(void *) &pin_name);
 
-	pin_retries = get_pin_retries(sim);
+	get_pin_retries(sim, &pin_retries_dict, &dbus_retries);
 	ofono_dbus_dict_append_dict(&dict, "Retries", DBUS_TYPE_BYTE,
-								&pin_retries);
-	g_free(pin_retries);
+							&pin_retries_dict);
+	g_free(pin_retries_dict);
+	g_free(dbus_retries);
 
 done:
 	dbus_message_iter_close_container(&iter, &dict);
@@ -437,7 +446,8 @@ static void sim_pin_retries_query_cb(const struct ofono_error *error,
 	struct ofono_sim *sim = data;
 	DBusConnection *conn = ofono_dbus_get_connection();
 	const char *path = __ofono_atom_get_path(sim->atom);
-	void **pin_retries;
+	void **pin_retries_dict;
+	unsigned char *dbus_retries;
 
 	if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
 		ofono_error("Querying remaining pin retries failed");
@@ -449,11 +459,12 @@ static void sim_pin_retries_query_cb(const struct ofono_error *error,
 
 	memcpy(sim->pin_retries, retries, sizeof(sim->pin_retries));
 
-	pin_retries = get_pin_retries(sim);
+	get_pin_retries(sim, &pin_retries_dict, &dbus_retries);
 	ofono_dbus_signal_dict_property_changed(conn, path,
 					OFONO_SIM_MANAGER_INTERFACE, "Retries",
-					DBUS_TYPE_BYTE,	&pin_retries);
-	g_free(pin_retries);
+					DBUS_TYPE_BYTE,	&pin_retries_dict);
+	g_free(pin_retries_dict);
+	g_free(dbus_retries);
 }
 
 static void sim_pin_retries_check(struct ofono_sim *sim)
Signature · 1 line
1.9.2
#3 lore raw

Hi Cedric,

Quoted 6 lines · Cedric Jehasse

Hi Denis,

Quoted 2 lines

Please try the following patch (attached) and tell me if it fixes this bug on your platform.

This patch caused a segfault. Please have a look at the reworked patch (attached).

Yes, of course. Thanks for cleaning up after me. The corrected fix has now been pushed upstream.

Regards,
-Denis