Debian Codegraph @

src/cbd-modem-3gpp.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2025 The Phosh.mobi e.V.
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * Author: Guido Günther <[email protected]>
7 */
8
9 #define G_LOG_DOMAIN "cbd-modem-3gpp"
10
11 #include "cbd-modem-3gpp.h"
12
13 #include "gmobile.h"
14
15 /**
16 * CbdModem3gpp:
17 *
18 * Object tracking ModemManagers's 3gpp interface
19 */
20
21 enum {
22 PROP_0,
23 PROP_MODEM_3GPP,
24 PROP_COUNTRY,
25 PROP_LAST_PROP
26 };
27 static GParamSpec *props[PROP_LAST_PROP];
28
29 struct _CbdModem3gpp {
30 GObject parent;
31
32 MMModem3gpp *modem_3gpp;
33 char *operator_code;
34 char *country;
35 };
36 G_DEFINE_TYPE (CbdModem3gpp, cbd_modem_3gpp, G_TYPE_OBJECT)
37
38
39 static void
40 on_operator_code_changed (CbdModem3gpp *self)
41 {
42 g_autoptr (GError) err = NULL;
43 const char *operator_code, *country;
44
45 g_assert (CBD_IS_MODEM_3GPP (self));
46 operator_code = mm_modem_3gpp_get_operator_code (self->modem_3gpp);
47
48 if (!g_strcmp0 (self->operator_code, operator_code))
49 return;
50
51 g_debug ("Operator is '%s'", operator_code);
52 g_set_str (&self->operator_code, operator_code);
53
54 if (!self->operator_code)
55 return;
56
57 country = gm_mcc_to_iso (self->operator_code, &err);
58 if (!country) {
59 g_warning ("Failed to determine country from '%s'", self->operator_code);
60 return;
61 }
62
63 if (!g_strcmp0 (self->country, country))
64 return;
65
66 g_set_str (&self->country, country);
67
68 g_object_notify_by_pspec (G_OBJECT (self), props[PROP_COUNTRY]);
69 }
70
71
72 static void
73 cbd_modem_3gpp_set_modem (CbdModem3gpp *self, MMModem3gpp *modem)
74 {
75 /* Construct only */
76 g_assert (self->modem_3gpp == NULL);
77
78 self->modem_3gpp = g_object_ref (modem);
79
80 g_signal_connect_swapped (self->modem_3gpp,
81 "notify::operator-code",
82 G_CALLBACK (on_operator_code_changed),
83 self);
84 on_operator_code_changed (self);
85 }
86
87
88 static void
89 cbd_modem_3gpp_set_property (GObject *object,
90 guint property_id,
91 const GValue *value,
92 GParamSpec *pspec)
93 {
94 CbdModem3gpp *self = CBD_MODEM_3GPP (object);
95
96 switch (property_id) {
97 case PROP_MODEM_3GPP:
98 cbd_modem_3gpp_set_modem (self, g_value_get_object (value));
99 break;
100 default:
101 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
102 break;
103 }
104 }
105
106
107 static void
108 cbd_modem_3gpp_get_property (GObject *object,
109 guint property_id,
110 GValue *value,
111 GParamSpec *pspec)
112 {
113 CbdModem3gpp *self = CBD_MODEM_3GPP (object);
114
115 switch (property_id) {
116 case PROP_MODEM_3GPP:
117 g_value_set_object (value, self->modem_3gpp);
118 break;
119 case PROP_COUNTRY:
120 g_value_set_string (value, cbd_modem_3gpp_get_country (self));
121 break;
122 default:
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
124 break;
125 }
126 }
127
128
129
130 static void
131 cbd_modem_3gpp_dispose (GObject *object)
132 {
133 CbdModem3gpp *self = CBD_MODEM_3GPP (object);
134
135 g_signal_handlers_disconnect_by_data (self->modem_3gpp, self);
136
137 g_clear_object (&self->modem_3gpp);
138
139 G_OBJECT_CLASS (cbd_modem_3gpp_parent_class)->dispose (object);
140 }
141
142
143 static void
144 cbd_modem_3gpp_finalize (GObject *object)
145 {
146 CbdModem3gpp *self = CBD_MODEM_3GPP (object);
147
148 g_clear_pointer (&self->operator_code, g_free);
149 g_clear_pointer (&self->country, g_free);
150
151 G_OBJECT_CLASS (cbd_modem_3gpp_parent_class)->finalize (object);
152 }
153
154
155 static void
156 cbd_modem_3gpp_class_init (CbdModem3gppClass *klass)
157 {
158 GObjectClass *object_class = G_OBJECT_CLASS (klass);
159
160 object_class->get_property = cbd_modem_3gpp_get_property;
161 object_class->set_property = cbd_modem_3gpp_set_property;
162 object_class->dispose = cbd_modem_3gpp_dispose;
163 object_class->finalize = cbd_modem_3gpp_finalize;
164
165 props[PROP_MODEM_3GPP] =
166 g_param_spec_object ("modem-3gpp", "", "",
167 MM_TYPE_MODEM_3GPP,
168 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
169
170 props[PROP_COUNTRY] =
171 g_param_spec_string ("country", "", "",
172 NULL,
173 G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
174
175 g_object_class_install_properties (object_class, PROP_LAST_PROP, props);
176 }
177
178
179 static void
180 cbd_modem_3gpp_init (CbdModem3gpp *self)
181 {
182 }
183
184
185 CbdModem3gpp *
186 cbd_modem_3gpp_new (MMModem3gpp *modem)
187 {
188 return g_object_new (CBD_TYPE_MODEM_3GPP, "modem-3gpp", modem, NULL);
189 }
190
191
192 gboolean
193 cbd_modem_3gpp_match (CbdModem3gpp *self, MMModem3gpp *modem_3gpp)
194 {
195 g_assert (CBD_IS_MODEM_3GPP (self));
196
197 return self->modem_3gpp == modem_3gpp;
198 }
199
200
201 const char *
202 cbd_modem_3gpp_get_country (CbdModem3gpp *self)
203 {
204 g_assert (CBD_IS_MODEM_3GPP (self));
205
206 return self->country;
207 }
208
209
210 const char *
211 cbd_modem_3gpp_get_operator_code (CbdModem3gpp *self)
212 {
213 g_assert (CBD_IS_MODEM_3GPP (self));
214
215 return self->operator_code;
216 }