16
16
*/
17
17
18
18
#include " UBLOX_AT.h"
19
- #include " UBLOX_AT_CellularNetwork.h"
20
- #include " UBLOX_AT_CellularContext.h"
21
19
22
20
using namespace mbed ;
23
21
using namespace events ;
@@ -72,7 +70,8 @@ AT_CellularNetwork *UBLOX_AT::open_network_impl(ATHandler &at)
72
70
73
71
AT_CellularContext *UBLOX_AT::create_context_impl (ATHandler &at, const char *apn, bool cp_req, bool nonip_req)
74
72
{
75
- return new UBLOX_AT_CellularContext (at, this , apn, cp_req, nonip_req);
73
+ ubx_context = new UBLOX_AT_CellularContext (at, this , apn, cp_req, nonip_req);
74
+ return ubx_context;
76
75
}
77
76
78
77
#if MBED_CONF_UBLOX_AT_PROVIDE_DEFAULT
@@ -89,3 +88,127 @@ CellularDevice *CellularDevice::get_default_instance()
89
88
}
90
89
#endif
91
90
91
+ nsapi_error_t UBLOX_AT::init ()
92
+ {
93
+ _at->lock ();
94
+ _at->flush ();
95
+ _at->cmd_start (" AT" );
96
+ _at->cmd_stop_read_resp ();
97
+
98
+ #ifdef TARGET_UBLOX_C027
99
+ _at->cmd_start (" AT+CFUN=0" );
100
+ _at->cmd_stop_read_resp ();
101
+ if (_at->get_last_error () == NSAPI_ERROR_OK) {
102
+ _at->cmd_start (" ATE0" ); // echo off
103
+ _at->cmd_stop_read_resp ();
104
+ _at->cmd_start (" AT+CMEE=1" ); // verbose responses
105
+ _at->cmd_stop_read_resp ();
106
+ config_authentication_parameters ();
107
+ _at->cmd_start (" AT+CFUN=1" ); // set full functionality
108
+ _at->cmd_stop_read_resp ();
109
+ }
110
+ #else
111
+ _at->cmd_start (" AT+CFUN=4" );
112
+ _at->cmd_stop_read_resp ();
113
+ if (_at->get_last_error () == NSAPI_ERROR_OK) {
114
+ _at->cmd_start (" ATE0" ); // echo off
115
+ _at->cmd_stop_read_resp ();
116
+ _at->cmd_start (" AT+CMEE=1" ); // verbose responses
117
+ _at->cmd_stop_read_resp ();
118
+ config_authentication_parameters ();
119
+ _at->cmd_start (" AT+CFUN=1" ); // set full functionality
120
+ _at->cmd_stop_read_resp ();
121
+ }
122
+ #endif
123
+
124
+ return _at->unlock_return_error ();
125
+ }
126
+
127
+ nsapi_error_t UBLOX_AT::config_authentication_parameters ()
128
+ {
129
+ char *config = NULL ;
130
+ nsapi_error_t err;
131
+ char imsi[MAX_IMSI_LENGTH + 1 ];
132
+
133
+ if (apn == NULL ) {
134
+ err = get_imsi (imsi);
135
+ if (err == NSAPI_ERROR_OK) {
136
+ config = (char *)apnconfig (imsi);
137
+ }
138
+ }
139
+
140
+ ubx_context->get_next_credentials (&config);
141
+ apn = ubx_context->get_apn ();
142
+ pwd = ubx_context->get_pwd ();
143
+ uname = ubx_context->get_uname ();
144
+ auth = ubx_context->get_auth ();
145
+
146
+ auth = (*uname && *pwd) ? auth : CellularContext::NOAUTH;
147
+ err = set_authentication_parameters (apn, uname, pwd, auth);
148
+
149
+ return err;
150
+ }
151
+
152
+ nsapi_error_t UBLOX_AT::set_authentication_parameters (const char *apn,
153
+ const char *username,
154
+ const char *password,
155
+ CellularContext::AuthenticationType auth)
156
+ {
157
+ int modem_security = ubx_context->nsapi_security_to_modem_security (auth);
158
+
159
+ _at->cmd_start (" AT+CGDCONT=1,\" IP\" ," );
160
+ _at->write_string (apn);
161
+ _at->cmd_stop ();
162
+ _at->resp_start ();
163
+ _at->resp_stop ();
164
+
165
+ if (_at->get_last_error () == NSAPI_ERROR_OK) {
166
+ #ifdef TARGET_UBLOX_C030_R41XM
167
+ if (modem_security == CellularContext::CHAP) {
168
+ _at->cmd_start (" AT+UAUTHREQ=1," );
169
+ _at->write_int (modem_security);
170
+ _at->write_string (password);
171
+ _at->write_string (username);
172
+ _at->cmd_stop ();
173
+ _at->resp_start ();
174
+ _at->resp_stop ();
175
+ } else if (modem_security == CellularContext::NOAUTH) {
176
+ _at->cmd_start (" AT+UAUTHREQ=1," );
177
+ _at->write_int (modem_security);
178
+ _at->cmd_stop ();
179
+ _at->resp_start ();
180
+ _at->resp_stop ();
181
+ } else {
182
+ _at->cmd_start (" AT+UAUTHREQ=1," );
183
+ _at->write_int (modem_security);
184
+ _at->write_string (username);
185
+ _at->write_string (password);
186
+ _at->cmd_stop ();
187
+ _at->resp_start ();
188
+ _at->resp_stop ();
189
+ }
190
+ #else
191
+ _at->cmd_start (" AT+UAUTHREQ=1," );
192
+ _at->write_int (modem_security);
193
+ _at->write_string (username);
194
+ _at->write_string (password);
195
+ _at->cmd_stop ();
196
+ _at->resp_start ();
197
+ _at->resp_stop ();
198
+ #endif
199
+ }
200
+
201
+ return _at->get_last_error ();
202
+ }
203
+
204
+ nsapi_error_t UBLOX_AT::get_imsi (char *imsi)
205
+ {
206
+ _at->lock ();
207
+ _at->cmd_start (" AT+CIMI" );
208
+ _at->cmd_stop ();
209
+ _at->resp_start ();
210
+ _at->read_string (imsi, MAX_IMSI_LENGTH + 1 );
211
+ _at->resp_stop ();
212
+
213
+ return _at->unlock_return_error ();
214
+ }
0 commit comments