Dawn Framework 1.0
Universal data acquisition framework for embedded systems
lte.cxx
1// dawn/src/system/lte.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/system/lte.hxx"
7
8#include <cerrno>
9#include <cstring>
10
11#include "dawn/debug.hxx"
12
13using namespace dawn;
14
15// Copy a DTYPE_CHAR config item payload into a null-terminated buffer.
16
17static void cfgCopyStr(char *dst, size_t dstsz, const SObjectCfg::SObjectCfgItem *item)
18{
19 size_t bytes = static_cast<size_t>(item->cfgid.s.size) * sizeof(uint32_t);
20 size_t n = (bytes < dstsz - 1) ? bytes : dstsz - 1;
21
22 std::memcpy(dst, reinterpret_cast<const char *>(&item->data), n);
23 dst[n] = '\0';
24}
25
26CSystemLte::CSystemLte(CDescObject &desc)
27 : CSystemCommon(desc)
28 , auth_type(0)
29 , ip_type(0)
30 , psave_mode(0)
31 , reg_timeout(0)
32{
33 apn[0] = '\0';
34 username[0] = '\0';
35 password[0] = '\0';
36}
37
38void CSystemLte::loadParams()
39{
40 CDescObject &desc = getDesc();
41 size_t count = desc.getSize();
42 size_t offset = 0;
43 size_t i;
45
46 // Kconfig defaults
47
48 std::strncpy(apn, CONFIG_DAWN_SYSTEM_LTE_APN, sizeof(apn) - 1);
49 apn[sizeof(apn) - 1] = '\0';
50 std::strncpy(username, CONFIG_DAWN_SYSTEM_LTE_USERNAME, sizeof(username) - 1);
51 username[sizeof(username) - 1] = '\0';
52 std::strncpy(password, CONFIG_DAWN_SYSTEM_LTE_PASSWORD, sizeof(password) - 1);
53 password[sizeof(password) - 1] = '\0';
54 auth_type = CONFIG_DAWN_SYSTEM_LTE_AUTHTYPE;
55 ip_type = CONFIG_DAWN_SYSTEM_LTE_IPTYPE;
56 psave_mode = CONFIG_DAWN_SYSTEM_LTE_PSAVE;
57 reg_timeout = CONFIG_DAWN_SYSTEM_LTE_REG_TIMEOUT;
58
59 // Override from descriptor config items
60
61 for (i = 0; i < count; i++)
62 {
63 item = desc.objectCfgItemNext(offset);
64 if (item == nullptr)
65 {
66 break;
67 }
68
69 if (item->cfgid.s.cls != SYSTEM_CLASS_LTE)
70 {
71 continue;
72 }
73
74 switch (item->cfgid.s.id)
75 {
76 case LTE_CFG_APN:
77 cfgCopyStr(apn, sizeof(apn), item);
78 break;
79
81 cfgCopyStr(username, sizeof(username), item);
82 break;
83
85 cfgCopyStr(password, sizeof(password), item);
86 break;
87
89 auth_type = static_cast<uint8_t>(item->data[0] & 0xff);
90 break;
91
92 case LTE_CFG_IPTYPE:
93 ip_type = static_cast<uint8_t>(item->data[0] & 0xff);
94 break;
95
97 reg_timeout = item->data[0];
98 break;
99
100 default:
101 break;
102 }
103 }
104}
105
107{
108 loadParams();
109
110 if (auth_type > DAWN_LTE_AUTH_CHAP || ip_type > DAWN_LTE_IPTYPE_V4V6)
111 {
112 DAWNERR("LTE invalid auth/ip config\n");
113 return -EINVAL;
114 }
115
116 return OK;
117}
118
120{
121 struct SLteParams params;
122 int ret;
123
124 loadParams();
125
126 params.apn = apn[0] ? apn : nullptr;
127 params.username = username[0] ? username : nullptr;
128 params.password = password[0] ? password : nullptr;
129 params.auth_type = auth_type;
130 params.ip_type = ip_type;
131 params.psave_mode = psave_mode;
132 params.reg_timeout = reg_timeout;
133
134 ret = lte_port_connect(&params);
135 if (ret < 0)
136 {
137 DAWNERR("LTE connect failed: %d\n", ret);
138 return ret;
139 }
140
141 DAWNINFO("LTE connected (apn=%s)\n", params.apn ? params.apn : "default");
142 return OK;
143}
144
146{
147 return lte_port_disconnect();
148}
Descriptor wrapper for individual object configuration.
size_t getSize() const
Get number of configuration items for this object.
SObjectCfg::SObjectCfgItem * objectCfgItemNext(size_t &offset) const
Get config item at current offset and advance past it.
CDescObject & getDesc()
Get descriptor object for this object.
Definition object.cxx:190
Base class for OBJTYPE_ANY configuration objects.
Definition common.hxx:25
@ SYSTEM_CLASS_LTE
LTE / cellular connectivity settings.
Definition common.hxx:30
int configure()
Configure object from descriptor data.
Definition lte.cxx:106
@ LTE_CFG_AUTHTYPE
DAWN_LTE_AUTH_* (DTYPE_UINT8)
Definition lte.hxx:32
@ LTE_CFG_USERNAME
APN username (DTYPE_CHAR)
Definition lte.hxx:30
@ LTE_CFG_IPTYPE
DAWN_LTE_IPTYPE_* (DTYPE_UINT8)
Definition lte.hxx:33
@ LTE_CFG_REG_TIMEOUT
Registration timeout, seconds (DTYPE_UINT32)
Definition lte.hxx:34
@ LTE_CFG_PASSWORD
APN password (DTYPE_CHAR)
Definition lte.hxx:31
@ LTE_CFG_APN
APN (DTYPE_CHAR)
Definition lte.hxx:29
int doStart()
Start implementation hook.
Definition lte.cxx:119
int doStop()
Stop implementation hook.
Definition lte.cxx:145
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
@ DAWN_LTE_AUTH_CHAP
CHAP.
Definition lte.hxx:20
@ DAWN_LTE_IPTYPE_V4V6
IPv4/IPv6.
Definition lte.hxx:31
Single configuration item within object.
ObjectCfgData_t data[]
Configuration data array (flexible, size from cfgid.s.size).
UObjectCfgId cfgid
Configuration ID header (type, class, id, size, rw, dtype).
uint32_t cls
Object class (bits 21-29, max 511).
uint32_t id
Configuration identifier (bits 0-4, max 31).
Definition objectcfg.hxx:94
uint32_t size
Configuration data size in 32-bit words (bits 5-14, max 1023).
struct dawn::SObjectCfg::UObjectCfgId::@10 s
Bit-field structure for named member access.