Merge pull request #12214 from kivaisan/move_string_to_pdp_type_to_cellularcontext

Cellular: Move string_to_pdp_type method to CellularContext
pull/12254/head
Martin Kojtal 2020-01-08 16:59:58 +01:00 committed by GitHub
commit 8d94d4ce92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 126 additions and 63 deletions

View File

@ -18,7 +18,6 @@
#include <string.h>
#include "CellularUtil.h"
using namespace mbed;
using namespace mbed_cellular_util;
// AStyle ignored as the definition is not clear due to preprocessor usage
@ -250,22 +249,3 @@ TEST_F(Testutil, int_to_hex_str)
EXPECT_TRUE(buf[0] == '6');
EXPECT_TRUE(buf[1] == '4');
}
TEST_F(Testutil, string_to_pdp_type)
{
pdp_type_t type = string_to_pdp_type("IPV4V6");
ASSERT_EQ(type, IPV4V6_PDP_TYPE);
type = string_to_pdp_type("IPV6");
ASSERT_EQ(type, IPV6_PDP_TYPE);
type = string_to_pdp_type("IP");
ASSERT_EQ(type, IPV4_PDP_TYPE);
type = string_to_pdp_type("Non-IP");
ASSERT_EQ(type, NON_IP_PDP_TYPE);
type = string_to_pdp_type("diipadaapa");
ASSERT_EQ(type, DEFAULT_PDP_TYPE);
}

View File

@ -47,12 +47,15 @@ public:
{
_device = dev;
_cp_netif = new ControlPlane_netif_stub();
nonip_pdp_string = NULL;
}
~testContext()
{
delete _cp_netif;
}
int get_retry_count()
{
return _retry_count;
@ -180,6 +183,11 @@ public:
}
const char *get_nonip_context_type_str()
{
return nonip_pdp_string;
}
void cp_data_received()
{
CellularContext::cp_data_received();
@ -198,6 +206,58 @@ public:
{
CellularContext::do_connect_with_retry();
}
void test_string_to_pdp_type()
{
pdp_type_t type = string_to_pdp_type("IPV4V6");
ASSERT_EQ(type, IPV4V6_PDP_TYPE);
type = string_to_pdp_type("IPV6");
ASSERT_EQ(type, IPV6_PDP_TYPE);
type = string_to_pdp_type("IP");
ASSERT_EQ(type, IPV4_PDP_TYPE);
type = string_to_pdp_type("Non-IP");
ASSERT_EQ(type, NON_IP_PDP_TYPE);
nonip_pdp_string = NULL;
type = string_to_pdp_type("diipadaapa");
ASSERT_EQ(type, DEFAULT_PDP_TYPE);
}
void test_nonip_context_type_str()
{
nonip_pdp_string = "NONIP";
pdp_type_t type = string_to_pdp_type("diipadaapa");
ASSERT_EQ(type, DEFAULT_PDP_TYPE);
type = string_to_pdp_type("NONIP");
ASSERT_EQ(type, NON_IP_PDP_TYPE);
type = string_to_pdp_type("nonip");
ASSERT_EQ(type, DEFAULT_PDP_TYPE);
type = string_to_pdp_type("IPV6");
ASSERT_EQ(type, IPV6_PDP_TYPE);
nonip_pdp_string = "testnonip";
type = string_to_pdp_type("diipadaapa");
ASSERT_EQ(type, DEFAULT_PDP_TYPE);
type = string_to_pdp_type("testnonip");
ASSERT_EQ(type, NON_IP_PDP_TYPE);
type = string_to_pdp_type("nonip");
ASSERT_EQ(type, DEFAULT_PDP_TYPE);
type = string_to_pdp_type("IPV6");
ASSERT_EQ(type, IPV6_PDP_TYPE);
}
const char *nonip_pdp_string;
};
static int network_cb_count = 0;
@ -314,6 +374,20 @@ TEST_F(TestCellularContext, do_connect_with_retry_async)
delete dev;
}
TEST_F(TestCellularContext, string_to_pdp_type)
{
testContext *ctx = new testContext();
EXPECT_TRUE(ctx != NULL);
ctx->test_string_to_pdp_type();
delete ctx;
}
TEST_F(TestCellularContext, nonip_context_type_str)
{
testContext *ctx = new testContext();
EXPECT_TRUE(ctx != NULL);
ctx->test_nonip_context_type_str();
delete ctx;
}

View File

@ -68,4 +68,9 @@ void CellularContext::call_network_cb(nsapi_connection_status_t status)
}
}
CellularContext::pdp_type_t CellularContext::string_to_pdp_type(const char *pdp_type)
{
return IPV4V6_PDP_TYPE;
}
}

View File

@ -28,7 +28,6 @@ int CellularUtil_stub::char_pos = 0;
char *CellularUtil_stub::char_table[50] = {};
int CellularUtil_stub::table_idx = 0;
using namespace mbed;
namespace mbed_cellular_util {
#define MAX_STRING_LEN 200
@ -134,9 +133,4 @@ uint16_t get_dynamic_ip_port()
return CellularUtil_stub::uint16_value;
}
pdp_type_t string_to_pdp_type(const char *pdp_type)
{
return IPV4V6_PDP_TYPE;
}
} // namespace mbed_cellular_util

View File

@ -305,6 +305,14 @@ protected: // Device specific implementations might need these so protected
OP_MAX = 5
};
enum pdp_type_t {
DEFAULT_PDP_TYPE = DEFAULT_STACK,
IPV4_PDP_TYPE = IPV4_STACK,
IPV6_PDP_TYPE = IPV6_STACK,
IPV4V6_PDP_TYPE = IPV4V6_STACK,
NON_IP_PDP_TYPE
};
/** The CellularDevice calls the status callback function on status changes on the network or CellularDevice.
*
* @param ev event type
@ -321,6 +329,16 @@ protected: // Device specific implementations might need these so protected
*/
virtual void enable_hup(bool enable) = 0;
/** Return PDP type string for Non-IP if modem uses other than standard "Non-IP"
*
* Some modems uses a non-standard PDP type string for non-ip (e.g. "NONIP").
* In those cases modem driver must implement this method to return the PDP type string
* used by the modem.
*
* @return PDP type string used by the modem or NULL if standard ("Non-IP")
*/
virtual const char *get_nonip_context_type_str() = 0;
/** Triggers control plane's operations needed when control plane data is received,
* like socket event, for example.
*/
@ -347,6 +365,14 @@ protected: // Device specific implementations might need these so protected
*/
void validate_ip_address();
/** Converts the given pdp type in char format to enum pdp_type_t
*
* @param pdp_type pdp type in string format
* @return converted pdp_type_t enum
*/
CellularContext::pdp_type_t string_to_pdp_type(const char *pdp_type);
protected:
// member variables needed in target override methods
NetworkStack *_stack; // must be pointer because of PPP
pdp_type_t _pdp_type;

View File

@ -25,7 +25,6 @@
#define RANDOM_PORT_NUMBER_COUNT (RANDOM_PORT_NUMBER_END - RANDOM_PORT_NUMBER_START + 1)
#define RANDOM_PORT_NUMBER_MAX_STEP 100
using namespace mbed;
namespace mbed_cellular_util {
nsapi_version_t convert_ipv6(char *ip)
@ -365,23 +364,4 @@ uint16_t get_dynamic_ip_port()
return (RANDOM_PORT_NUMBER_START + port_counter);
}
pdp_type_t string_to_pdp_type(const char *pdp_type_str)
{
pdp_type_t pdp_type = DEFAULT_PDP_TYPE;
int len = strlen(pdp_type_str);
if (len == 6 && memcmp(pdp_type_str, "IPV4V6", len) == 0) {
pdp_type = IPV4V6_PDP_TYPE;
} else if (len == 4 && memcmp(pdp_type_str, "IPV6", len) == 0) {
pdp_type = IPV6_PDP_TYPE;
} else if (len == 2 && memcmp(pdp_type_str, "IP", len) == 0) {
pdp_type = IPV4_PDP_TYPE;
} else if (len == 6 && memcmp(pdp_type_str, "Non-IP", len) == 0) {
pdp_type = NON_IP_PDP_TYPE;
} else if (len == 5 && memcmp(pdp_type_str, "NONIP", len) == 0) {
pdp_type = NON_IP_PDP_TYPE;
}
return pdp_type;
}
} // namespace mbed_cellular_util

View File

@ -22,16 +22,6 @@
#include <inttypes.h>
#include "nsapi_types.h"
namespace mbed {
typedef enum pdp_type {
DEFAULT_PDP_TYPE = DEFAULT_STACK,
IPV4_PDP_TYPE = IPV4_STACK,
IPV6_PDP_TYPE = IPV6_STACK,
IPV4V6_PDP_TYPE = IPV4V6_STACK,
NON_IP_PDP_TYPE
} pdp_type_t;
}
namespace mbed_cellular_util {
// some helper macros
@ -139,13 +129,6 @@ uint32_t binary_str_to_uint(const char *binary_string, int binary_string_length)
*/
uint16_t get_dynamic_ip_port();
/** Converts the given pdp type in char format to enum pdp_type_t
*
* @param pdp_type pdp type in string format
* @return converted pdp_type_t enum
*/
mbed::pdp_type_t string_to_pdp_type(const char *pdp_type);
} // namespace mbed_cellular_util
#endif

View File

@ -113,6 +113,27 @@ void CellularContext::validate_ip_address()
}
}
CellularContext::pdp_type_t CellularContext::string_to_pdp_type(const char *pdp_type_str)
{
pdp_type_t pdp_type = DEFAULT_PDP_TYPE;
int len = strlen(pdp_type_str);
if (len == 6 && memcmp(pdp_type_str, "IPV4V6", len) == 0) {
pdp_type = IPV4V6_PDP_TYPE;
} else if (len == 4 && memcmp(pdp_type_str, "IPV6", len) == 0) {
pdp_type = IPV6_PDP_TYPE;
} else if (len == 2 && memcmp(pdp_type_str, "IP", len) == 0) {
pdp_type = IPV4_PDP_TYPE;
} else if (len == 6 && memcmp(pdp_type_str, "Non-IP", len) == 0) {
pdp_type = NON_IP_PDP_TYPE;
} else if (get_nonip_context_type_str() &&
len == strlen(get_nonip_context_type_str()) &&
memcmp(pdp_type_str, get_nonip_context_type_str(), len) == 0) {
pdp_type = NON_IP_PDP_TYPE;
}
return pdp_type;
}
void CellularContext::do_connect_with_retry()
{
if (_cb_data.final_try) {