Skip to content

Commit fcfe6e1

Browse files
author
Cruz Monrreal
authored
Merge pull request #7217 from AriParkkila/cellular-info-sim
Cellular: Add AT+CCID and AT+CGSN
2 parents d1dc1e8 + 7d006f6 commit fcfe6e1

14 files changed

+237
-36
lines changed

features/cellular/framework/API/CellularInformation.h

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ namespace mbed {
2828
*
2929
* An abstract interface that provides information about cellular device.
3030
*/
31-
class CellularInformation
32-
{
31+
class CellularInformation {
3332
protected:
3433
// friend of CellularDevice so that it's the only way to close/delete this class.
3534
friend class CellularDevice;
@@ -41,27 +40,42 @@ class CellularInformation
4140
public:
4241
/** Request manufacturer identification of cellular device
4342
*
44-
* @param buf manufacturer identification
43+
* @param buf manufacturer identification as zero terminated string
4544
* @param buf_size max length of manufacturer identification is 2048 characters
46-
* @return on success read character count, on failure negative error code
45+
* @return zero on success, on failure negative error code
4746
*/
48-
virtual nsapi_size_or_error_t get_manufacturer(char *buf, size_t buf_size) = 0;
47+
virtual nsapi_error_t get_manufacturer(char *buf, size_t buf_size) = 0;
4948

5049
/** Request model identification of cellular device
5150
*
52-
* @param buf model identification
51+
* @param buf model identification as zero terminated string
5352
* @param buf_size max length of model identification is 2048 characters
54-
* @return on success read character count, on failure negative error code
53+
* @return zero on success, on failure negative error code
5554
*/
56-
virtual nsapi_size_or_error_t get_model(char *buf, size_t buf_size) = 0;
55+
virtual nsapi_error_t get_model(char *buf, size_t buf_size) = 0;
5756

5857
/** Request revision identification of cellular device
5958
*
60-
* @param buf revision identification
59+
* @param buf revision identification as zero terminated string
6160
* @param buf_size max length of revision identification is 2048 characters
62-
* @return on success read character count, on failure negative error code
61+
* @return zero on success, on failure negative error code
6362
*/
64-
virtual nsapi_size_or_error_t get_revision(char *buf, size_t buf_size) = 0;
63+
virtual nsapi_error_t get_revision(char *buf, size_t buf_size) = 0;
64+
65+
/** Request serial number identification of cellular device
66+
*
67+
* @param buf serial number as zero terminated string
68+
* @param buf_size max length of serial number is 2048 characters
69+
* @param type serial number type to read
70+
* @return zero on success, on failure negative error code
71+
*/
72+
enum SerialNumberType {
73+
SN = 0, // Serial Number
74+
IMEI = 1, // International Mobile station Equipment Identity
75+
IMEISV = 2, // IMEI and Software Version number
76+
SVN = 3 // Software Version Number
77+
};
78+
virtual nsapi_size_or_error_t get_serial_number(char *buf, size_t buf_size, SerialNumberType type = SN) = 0;
6579
};
6680

6781
} // namespace mbed

features/cellular/framework/API/CellularSIM.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@
1818
#ifndef CELLULAR_SIM_H_
1919
#define CELLULAR_SIM_H_
2020

21+
#include <stddef.h>
22+
2123
#include "nsapi_types.h"
2224

2325
namespace mbed {
2426

2527
const int MAX_SIM_READY_WAITING_TIME = 30;
2628
const int MAX_IMSI_LENGTH = 15;
29+
const int MAX_ICCID_LENGTH = 20 + 1; // +1 for zero termination
2730
/**
2831
* Class CellularSIM
2932
*
3033
* An abstract interface for SIM card handling.
3134
*/
32-
class CellularSIM
33-
{
35+
class CellularSIM {
3436
protected:
3537
// friend of CellularDevice so that it's the only way to close/delete this class.
3638
friend class CellularDevice;
@@ -85,7 +87,15 @@ class CellularSIM
8587
* @param imsi preallocated char* which after successful request contains imsi
8688
* @return zero on success
8789
*/
88-
virtual nsapi_error_t get_imsi(char* imsi) = 0;
90+
virtual nsapi_error_t get_imsi(char *imsi) = 0;
91+
92+
/** Get serial number from the SIM card
93+
*
94+
* @param buf SIM ICCID as zero terminated string
95+
* @param buf_size max length of SIM ICCID is MAX_ICCID_LENGTH
96+
* @return zero on success, on failure negative error code
97+
*/
98+
virtual nsapi_error_t get_iccid(char *buf, size_t buf_size) = 0;
8999
};
90100

91101
} // namespace mbed

features/cellular/framework/AT/AT_CellularBase.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
*/
1717

1818
#include "AT_CellularBase.h"
19+
#include "CellularLog.h"
1920

2021
using namespace mbed;
2122

22-
AT_CellularBase::AT_CellularBase(ATHandler& at) : _at(at)
23+
AT_CellularBase::AT_CellularBase(ATHandler &at) : _at(at)
2324
{
24-
2525
}
2626

27-
ATHandler& AT_CellularBase::get_at_handler()
27+
ATHandler &AT_CellularBase::get_at_handler()
2828
{
2929
return _at;
3030
}
@@ -34,3 +34,25 @@ device_err_t AT_CellularBase::get_device_error() const
3434
return _at.get_last_device_error();
3535
}
3636

37+
AT_CellularBase::SupportedFeature const *AT_CellularBase::_unsupported_features;
38+
39+
void AT_CellularBase::set_unsupported_features(const SupportedFeature *unsupported_features)
40+
{
41+
_unsupported_features = unsupported_features;
42+
}
43+
44+
bool AT_CellularBase::is_supported(SupportedFeature feature)
45+
{
46+
if (!_unsupported_features) {
47+
return true;
48+
}
49+
50+
for (int i = 0; _unsupported_features[i] != SUPPORTED_FEATURE_END_MARK; i++) {
51+
if (_unsupported_features[i] == feature) {
52+
tr_debug("Unsupported feature (%d)", (int)feature);
53+
return false;
54+
}
55+
}
56+
57+
return true;
58+
}

features/cellular/framework/AT/AT_CellularBase.h

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@ namespace mbed {
2626
*
2727
* A base class for AT-classes.
2828
*/
29-
class AT_CellularBase
30-
{
29+
class AT_CellularBase {
3130
public:
32-
AT_CellularBase(ATHandler& at);
31+
AT_CellularBase(ATHandler &at);
3332

3433
/** Getter for at handler. Common method for all AT-classes.
3534
*
3635
* @return reference to ATHandler
3736
*/
38-
ATHandler& get_at_handler();
37+
ATHandler &get_at_handler();
3938

4039
/** Gets the device error that happened when using AT commands/responses. This is at error
4140
* returned by the device. Returned CME/CMS errors can be found from 3gpp documents 27007 and 27005.
@@ -44,8 +43,28 @@ class AT_CellularBase
4443
*/
4544
device_err_t get_device_error() const;
4645

46+
/** Cellular module need to define an array of unsupported features if any,
47+
* by default all features are supported.
48+
*
49+
* @param features Array of type SupportedFeature with last element FEATURE_END_MARK
50+
*/
51+
enum SupportedFeature {
52+
AT_CGSN_WITH_TYPE, // AT+CGSN without type is likely always supported similar to AT+GSN
53+
SUPPORTED_FEATURE_END_MARK // must be last element in the array of features
54+
};
55+
static void set_unsupported_features(const SupportedFeature *unsupported_features);
56+
4757
protected:
48-
ATHandler& _at;
58+
ATHandler &_at;
59+
60+
/** Check if some functionality is supported by a cellular module. For example,
61+
* most of standard AT commands are optional and not implemented by all cellular modules.
62+
*
63+
* @param feature check for feature to support
64+
* @return true on supported, otherwise false
65+
*/
66+
static const SupportedFeature *_unsupported_features;
67+
static bool is_supported(SupportedFeature feature);
4968
};
5069

5170
} // namespace mbed

features/cellular/framework/AT/AT_CellularInformation.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
#include <stdio.h>
19+
1820
#include "AT_CellularInformation.h"
1921

2022
using namespace mbed;
@@ -42,6 +44,26 @@ nsapi_error_t AT_CellularInformation::get_revision(char *buf, size_t buf_size)
4244
return get_info("AT+CGMR", buf, buf_size);
4345
}
4446

47+
nsapi_error_t AT_CellularInformation::get_serial_number(char *buf, size_t buf_size, SerialNumberType type)
48+
{
49+
if (type == SN) {
50+
return get_info("AT+CGSN", buf, buf_size);
51+
}
52+
53+
if (!is_supported(AT_CGSN_WITH_TYPE)) {
54+
return NSAPI_ERROR_UNSUPPORTED;
55+
}
56+
57+
_at.lock();
58+
_at.cmd_start("AT+CGSN=");
59+
_at.write_int(type);
60+
_at.cmd_stop();
61+
_at.resp_start("+CGSN:");
62+
_at.read_string(buf, buf_size);
63+
_at.resp_stop();
64+
return _at.unlock_return_error();
65+
}
66+
4567
nsapi_error_t AT_CellularInformation::get_info(const char *cmd, char *buf, size_t buf_size)
4668
{
4769
_at.lock();
@@ -50,7 +72,7 @@ nsapi_error_t AT_CellularInformation::get_info(const char *cmd, char *buf, size_
5072
_at.cmd_stop();
5173
_at.set_delimiter(0);
5274
_at.resp_start();
53-
_at.read_string(buf, buf_size-1);
75+
_at.read_string(buf, buf_size - 1);
5476
_at.resp_stop();
5577
_at.set_default_delimiter();
5678

features/cellular/framework/AT/AT_CellularInformation.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,26 @@ namespace mbed {
2828
*
2929
* Class that provides information about cellular device.
3030
*/
31-
class AT_CellularInformation : public CellularInformation, public AT_CellularBase
32-
{
31+
class AT_CellularInformation : public CellularInformation, public AT_CellularBase {
3332
public:
3433
AT_CellularInformation(ATHandler &atHandler);
3534
virtual ~AT_CellularInformation();
3635

3736
public:
38-
virtual nsapi_size_or_error_t get_manufacturer(char *buf, size_t buf_size);
37+
virtual nsapi_error_t get_manufacturer(char *buf, size_t buf_size);
3938

40-
virtual nsapi_size_or_error_t get_model(char *buf, size_t buf_size);
39+
virtual nsapi_error_t get_model(char *buf, size_t buf_size);
4140

42-
virtual nsapi_size_or_error_t get_revision(char *buf, size_t buf_size);
41+
virtual nsapi_error_t get_revision(char *buf, size_t buf_size);
42+
43+
virtual nsapi_error_t get_serial_number(char *buf, size_t buf_size, SerialNumberType type);
4344

4445
protected:
4546
/** Request information text from cellular device
4647
*
4748
* @param cmd 3gpp command string
48-
* @param buf manufacturer identification
49-
* @param buf_size max length of manufacturer identification is 2048 characters
49+
* @param buf buffer for response
50+
* @param buf_size buffer size
5051
* @return on success read character count, on failure negative error code
5152
*/
5253
nsapi_error_t get_info(const char *cmd, char *buf, size_t buf_size);

features/cellular/framework/AT/AT_CellularSIM.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,14 @@ nsapi_error_t AT_CellularSIM::get_imsi(char *imsi)
150150
_at.resp_stop();
151151
return _at.unlock_return_error();
152152
}
153+
154+
nsapi_error_t AT_CellularSIM::get_iccid(char *buf, size_t buf_size)
155+
{
156+
_at.lock();
157+
_at.cmd_start("AT+CCID?");
158+
_at.cmd_stop();
159+
_at.resp_start("+CCID:");
160+
_at.read_string(buf, buf_size);
161+
_at.resp_stop();
162+
return _at.unlock_return_error();
163+
}

features/cellular/framework/AT/AT_CellularSIM.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ namespace mbed {
2828
*
2929
* Class for SIM card handling.
3030
*/
31-
class AT_CellularSIM : public CellularSIM, public AT_CellularBase
32-
{
31+
class AT_CellularSIM : public CellularSIM, public AT_CellularBase {
3332

3433
public:
3534
AT_CellularSIM(ATHandler &atHandler);
@@ -44,7 +43,9 @@ class AT_CellularSIM : public CellularSIM, public AT_CellularBase
4443

4544
virtual nsapi_error_t get_sim_state(SimState &state);
4645

47-
virtual nsapi_error_t get_imsi(char* imsi);
46+
virtual nsapi_error_t get_imsi(char *imsi);
47+
48+
virtual nsapi_error_t get_iccid(char *buf, size_t buf_size);
4849
};
4950

5051
} // namespace mbed

features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularSIM.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,14 @@ nsapi_error_t QUECTEL_BC95_CellularSIM::get_sim_state(SimState &state)
4747
return _at.unlock_return_error();
4848
}
4949

50+
// According to BC95_AT_Commands_Manual_V1.9
51+
nsapi_error_t QUECTEL_BC95_CellularSIM::get_iccid(char *buf, size_t buf_size)
52+
{
53+
_at.lock();
54+
_at.cmd_start("AT+NCCID?");
55+
_at.cmd_stop();
56+
_at.resp_start("+NCCID:");
57+
_at.read_string(buf, buf_size);
58+
_at.resp_stop();
59+
return _at.unlock_return_error();
60+
}

features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularSIM.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222

2323
namespace mbed {
2424

25-
class QUECTEL_BC95_CellularSIM : public AT_CellularSIM
26-
{
25+
class QUECTEL_BC95_CellularSIM : public AT_CellularSIM {
2726
public:
2827
QUECTEL_BC95_CellularSIM(ATHandler &atHandler);
2928
virtual ~QUECTEL_BC95_CellularSIM();
3029

3130
public: //from CellularSIM
3231
virtual nsapi_error_t get_sim_state(SimState &state);
32+
virtual nsapi_error_t get_iccid(char *buf, size_t buf_size);
3333
};
3434

3535
} // namespace mbed

0 commit comments

Comments
 (0)