-
Notifications
You must be signed in to change notification settings - Fork 513
/
system_network_manager.cpp
899 lines (773 loc) · 25.9 KB
/
system_network_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
/*
* Copyright (c) 2018 Particle Industries, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "logging.h"
LOG_SOURCE_CATEGORY("system.nm")
#undef LOG_COMPILE_TIME_LEVEL
#define LOG_COMPILE_TIME_LEVEL LOG_LEVEL_ALL
#include "hal_platform.h"
#if HAL_PLATFORM_IFAPI
#include "system_network_manager.h"
#include "system_error.h"
#include <mutex>
#include "system_led_signal.h"
#include "enumclass.h"
#if HAL_PLATFORM_WIFI && HAL_PLATFORM_NCP
#include "network/ncp/wifi/ncp.h"
#include "network/ncp/wifi/wifi_network_manager.h"
#endif // HAL_PLATFORM_WIFI && HAL_PLATFORM_NCP
#if HAL_PLATFORM_NCP && HAL_PLATFORM_CELLULAR
#include "cellular_hal.h"
#endif // HAL_PLATFORM_NCP && HAL_PLATFORM_CELLULAR
#include "check.h"
#include "system_cloud.h"
#include "system_threading.h"
#include "system_event.h"
#include "timer_hal.h"
#include "delay_hal.h"
#define CHECKV(_expr) \
({ \
const auto _ret = _expr; \
if (_ret < 0) { \
return; \
} \
_ret; \
})
#define CHECK_CONT(_expr) \
({ \
const auto _ret = _expr; \
if (_ret < 0) { \
continue; \
} \
_ret; \
})
namespace particle { namespace system {
namespace {
template <typename F>
int for_each_iface(F&& f) {
if_list* ifs = nullptr;
CHECK(if_get_list(&ifs));
for (if_list* iface = ifs; iface != nullptr; iface = iface->next) {
if (iface->iface) {
unsigned int flags = 0;
CHECK_CONT(if_get_flags(iface->iface, &flags));
if (!(flags & IFF_LOOPBACK)) {
f(iface->iface, flags);
}
}
}
if_free_list(ifs);
return 0;
}
void forceCloudPingIfConnected() {
const auto task = new(std::nothrow) ISRTaskQueue::Task();
if (!task) {
return;
}
task->func = [](ISRTaskQueue::Task* task) {
delete task;
if (spark_cloud_flag_connected()) {
spark_protocol_command(system_cloud_protocol_instance(), ProtocolCommands::PING, 0, nullptr);
}
};
SystemISRTaskQueue.enqueue(task);
}
} /* anonymous */
NetworkManager::NetworkManager() {
state_ = State::NONE;
ip4State_ = ProtocolState::UNCONFIGURED;
ip6State_ = ProtocolState::UNCONFIGURED;
dns4State_ = DnsState::UNCONFIGURED;
dns6State_ = DnsState::UNCONFIGURED;
}
NetworkManager::~NetworkManager() {
destroy();
}
NetworkManager* NetworkManager::instance() {
static NetworkManager man;
return &man;
}
void NetworkManager::init() {
if (!ifEventHandlerCookie_) {
ifEventHandlerCookie_ = if_event_handler_add(&ifEventHandlerCb, this);
}
if (!resolvEventHandlerCookie_) {
resolvEventHandlerCookie_ = resolv_event_handler_add(&resolvEventHandlerCb, this);
}
transition(State::DISABLED);
}
void NetworkManager::destroy() {
if (ifEventHandlerCookie_) {
if_event_handler_del(ifEventHandlerCookie_);
ifEventHandlerCookie_ = nullptr;
}
if (resolvEventHandlerCookie_) {
resolv_event_handler_del(resolvEventHandlerCookie_);
resolvEventHandlerCookie_ = nullptr;
}
}
int NetworkManager::enableNetworking() {
/* TODO: this should power on all the network hardware if possible */
if (state_ == State::DISABLED) {
transition(State::IFACE_DOWN);
}
return SYSTEM_ERROR_INVALID_STATE;
}
int NetworkManager::disableNetworking() {
/* TODO: this should power off all the network hardware if possible */
switch (state_) {
case State::DISABLED: {
return 0;
}
case State::IFACE_DOWN: {
/* Networking is enabled, but no interfaces were brought up. Nothing to do here */
transition(State::DISABLED);
break;
}
}
return SYSTEM_ERROR_INVALID_STATE;
}
bool NetworkManager::isNetworkingEnabled() const {
return state_ != State::DISABLED;
}
int NetworkManager::activateConnections() {
if (state_ != State::IFACE_DOWN) {
return SYSTEM_ERROR_INVALID_STATE;
}
/* Get a list of network interfaces */
transition(State::IFACE_REQUEST_UP);
int waitingFor = 0;
/* Bring all the interfaces up */
CHECK(for_each_iface([&](if_t iface, unsigned int flags) {
/* Skip interfaces that don't have configuration */
if (!haveLowerLayerConfiguration(iface)) {
return;
}
// Ignore disabled interfaces
if (!isInterfaceEnabled(iface)) {
return;
}
if (!(flags & IFF_UP)) {
CHECKV(if_set_flags(iface, IFF_UP));
/* FIXME */
CHECKV(if_set_xflags(iface, IFXF_DHCP));
/* TODO: establish lower layer connection, e.g. 802.11 */
++waitingFor;
}
}));
if (!waitingFor) {
/* No interfaces needed to be brought up */
transition(State::IFACE_UP);
}
return 0;
}
int NetworkManager::deactivateConnections() {
switch (state_) {
case State::DISABLED:
case State::IFACE_DOWN: {
return SYSTEM_ERROR_INVALID_STATE;
}
default: {
break;
}
}
transition(State::IFACE_REQUEST_DOWN);
int waitingFor = 0;
/* Bring all the interfaces down */
CHECK(for_each_iface([&](if_t iface, unsigned int flags) {
/* Skip interfaces that don't have configuration
* FIXME: Do we need this here as well?
*/
// if (!haveLowerLayerConfiguration(iface)) {
// return;
// }
if (flags & IFF_UP) {
CHECKV(if_clear_flags(iface, IFF_UP));
++waitingFor;
}
}));
if (!waitingFor) {
/* No interfaces needed to be brought down */
transition(State::IFACE_DOWN);
}
return 0;
}
bool NetworkManager::isEstablishingConnections() const {
switch (state_) {
case State::IFACE_REQUEST_UP:
case State::IFACE_UP:
case State::IFACE_LINK_UP: {
return true;
}
}
return false;
}
bool NetworkManager::isConnectivityAvailable() const {
return state_ == State::IP_CONFIGURED;
}
bool NetworkManager::isIp4ConnectivityAvailable() const {
return ip4State_ == ProtocolState::CONFIGURED;
}
bool NetworkManager::isIp6ConnectivityAvailable() const {
return ip6State_ == ProtocolState::CONFIGURED;
}
bool NetworkManager::isConfigured(if_t iface) const {
bool ret = false;
if (!iface) {
for_each_iface([&](if_t iface, unsigned int curFlags) {
if (haveLowerLayerConfiguration(iface)) {
ret = true;
}
});
return ret;
} else {
return haveLowerLayerConfiguration(iface);
}
}
int NetworkManager::clearConfiguration(if_t oIface) {
if (!isConfigured()) {
return SYSTEM_ERROR_NONE;
}
int ret = SYSTEM_ERROR_INVALID_ARGUMENT;
for_each_iface([&](if_t iface, unsigned int flags) {
char name[IF_NAMESIZE] = {};
if_get_name(iface, name);
if (oIface && iface != oIface) {
return;
}
#if HAL_PLATFORM_NCP && HAL_PLATFORM_WIFI
else if (!strncmp(name, "wl", 2)) {
auto wifiMan = wifiNetworkManager();
wifiMan->clearNetworkConfig();
ret = SYSTEM_ERROR_NONE;
}
#endif // HAL_PLATFORM_NCP && HAL_PLATFORM_WIFI
#if HAL_PLATFORM_NCP && HAL_PLATFORM_CELLULAR
else if (!strncmp(name, "pp", 2)) {
ret = cellular_credentials_clear(nullptr);
}
#endif // HAL_PLATFORM_NCP && HAL_PLATFORM_CELLULAR
});
if (!ret) {
system_notify_event(network_credentials, network_credentials_cleared);
}
return ret;
}
NetworkManager::State NetworkManager::getState() const {
return state_;
}
void NetworkManager::transition(State state) {
/* From */
switch (state_) {
case State::DISABLED: {
break;
}
case State::IFACE_DOWN: {
break;
}
case State::IFACE_REQUEST_DOWN: {
break;
}
case State::IFACE_REQUEST_UP: {
break;
}
case State::IFACE_UP: {
break;
}
case State::IFACE_LINK_UP: {
break;
}
/* Ensure that IPv4/IPv6 protocol state is reset */
case State::IP_CONFIGURED: {
if (state != State::IP_CONFIGURED) {
ip4State_ = ProtocolState::UNCONFIGURED;
ip6State_ = ProtocolState::UNCONFIGURED;
dns4State_ = DnsState::UNCONFIGURED;
dns6State_ = DnsState::UNCONFIGURED;
}
break;
}
}
/* To */
switch (state) {
case State::DISABLED: {
LED_SIGNAL_START(NETWORK_OFF, BACKGROUND);
// FIXME:
system_notify_event(network_status, network_status_powering_off);
system_notify_event(network_status, network_status_off);
break;
}
case State::IFACE_DOWN: {
LED_SIGNAL_START(NETWORK_ON, BACKGROUND);
if (state_ == State::IFACE_REQUEST_DOWN) {
system_notify_event(network_status, network_status_disconnected);
} else if (state_ == State::DISABLED) {
// FIXME:
system_notify_event(network_status, network_status_powering_on);
system_notify_event(network_status, network_status_on);
}
break;
}
case State::IFACE_REQUEST_DOWN: {
system_notify_event(network_status, network_status_disconnecting);
break;
}
case State::IFACE_REQUEST_UP: {
LED_SIGNAL_START(NETWORK_CONNECTING, BACKGROUND);
break;
}
case State::IFACE_UP: {
LED_SIGNAL_START(NETWORK_CONNECTING, BACKGROUND);
system_notify_event(network_status, network_status_connecting);
break;
}
case State::IFACE_LINK_UP: {
LED_SIGNAL_START(NETWORK_DHCP, BACKGROUND);
break;
}
case State::IP_CONFIGURED: {
LED_SIGNAL_START(NETWORK_CONNECTED, BACKGROUND);
if (state_ != State::IP_CONFIGURED) {
system_notify_event(network_status, network_status_connected);
}
break;
}
}
LOG(INFO, "State changed: %s -> %s", stateToName(state_), stateToName(state));
state_ = state;
}
void NetworkManager::ifEventHandlerCb(void* arg, if_t iface, const struct if_event* ev) {
auto self = static_cast<NetworkManager*>(arg);
self->ifEventHandler(iface, ev);
}
void NetworkManager::ifEventHandler(if_t iface, const struct if_event* ev) {
switch (ev->ev_type) {
case IF_EVENT_IF_ADDED: {
handleIfAdded(iface, ev);
break;
}
case IF_EVENT_IF_REMOVED: {
handleIfRemoved(iface, ev);
break;
}
case IF_EVENT_STATE: {
handleIfState(iface, ev);
break;
}
case IF_EVENT_LINK: {
handleIfLink(iface, ev);
break;
}
case IF_EVENT_ADDR: {
handleIfAddr(iface, ev);
break;
}
case IF_EVENT_LLADDR: {
handleIfLinkLayerAddr(iface, ev);
break;
}
case IF_EVENT_POWER_STATE: {
handleIfPowerState(iface, ev);
break;
}
}
}
void NetworkManager::handleIfAdded(if_t iface, const struct if_event* ev) {
/* TODO */
}
void NetworkManager::handleIfRemoved(if_t iface, const struct if_event* ev) {
/* TODO */
}
void NetworkManager::handleIfState(if_t iface, const struct if_event* ev) {
if (ev->ev_if_state->state) {
/* Interface administrative state changed to UP */
if (state_ == State::IFACE_REQUEST_UP) {
transition(State::IFACE_UP);
/* FIXME: is this needed? */
if (countIfacesWithFlags(IFF_LOWER_UP) > 0) {
transition(State::IFACE_LINK_UP);
}
}
} else {
/* Interface administrative state changed to DOWN */
if (state_ == State::IFACE_REQUEST_DOWN) {
/* FIXME: LwIP issues netif_set_down callback BEFORE clearing the IFF_UP flag,
* that's why the count of interfaces with IFF_UP should be 1 here
*/
if (countIfacesWithFlags(IFF_UP) <= 1) {
transition(State::IFACE_DOWN);
}
}
}
}
void NetworkManager::handleIfLink(if_t iface, const struct if_event* ev) {
if (ev->ev_if_link->state) {
/* Interface link state changed to UP */
if (state_ == State::IFACE_UP) {
transition(State::IFACE_LINK_UP);
refreshIpState();
} else if (state_ == State::IP_CONFIGURED || state_ == State::IFACE_LINK_UP) {
refreshIpState();
}
} else {
resetInterfaceProtocolState(iface);
/* Interface link state changed to DOWN */
if (state_ == State::IP_CONFIGURED || state_ == State::IFACE_LINK_UP) {
if (countIfacesWithFlags(IFF_UP | IFF_LOWER_UP) == 0) {
transition(State::IFACE_UP);
} else {
refreshIpState();
}
}
}
forceCloudPingIfConnected();
}
void NetworkManager::handleIfAddr(if_t iface, const struct if_event* ev) {
if (state_ == State::IP_CONFIGURED || state_ == State::IFACE_LINK_UP) {
refreshIpState();
}
forceCloudPingIfConnected();
}
void NetworkManager::handleIfLinkLayerAddr(if_t iface, const struct if_event* ev) {
/* We don't care about this */
}
unsigned int NetworkManager::countIfacesWithFlags(unsigned int flags) const {
unsigned int count = 0;
for_each_iface([&](if_t iface, unsigned int curFlags) {
/* Skip interfaces that don't have configuration */
if (!haveLowerLayerConfiguration(iface)) {
return;
}
// Ignore disabled interfaces
if (!isInterfaceEnabled(iface)) {
return;
}
if ((curFlags & flags) == flags) {
++count;
}
});
return count;
}
void NetworkManager::handleIfPowerState(if_t iface, const struct if_event* ev) {
// FIXME: this is not really thread-safe, but should probably work
auto state = getInterfaceRuntimeState(iface);
if (!state) {
LOG(ERROR, "Interface is not populated");
return;
}
if (state->pwrState != ev->ev_power_state->state) {
state->pwrState = static_cast<if_power_state_t>(ev->ev_power_state->state);
uint8_t index;
if_get_index(iface, &index);
LOG(TRACE, "Interface %d power state changed: %d", index, state->pwrState.load());
}
}
void NetworkManager::refreshIpState() {
ProtocolState ip4 = ProtocolState::UNCONFIGURED;
ProtocolState ip6 = ProtocolState::UNCONFIGURED;
resetInterfaceProtocolState();
if_addrs* addrs = nullptr;
CHECKV(if_get_if_addrs(&addrs));
for (auto addr = addrs; addr != nullptr; addr = addr->next) {
ProtocolState ifIp4 = ProtocolState::UNCONFIGURED;
ProtocolState ifIp6 = ProtocolState::UNCONFIGURED;
/* Skip loopback interface */
if (addr->ifflags & IFF_LOOPBACK) {
continue;
}
/* Skip non-UP and non-LINK_UP interfaces */
if ((addr->ifflags & (IFF_UP | IFF_LOWER_UP)) != (IFF_UP | IFF_LOWER_UP)) {
continue;
}
auto a = addr->if_addr;
if (!a || !a->addr) {
continue;
}
if (a->addr->sa_family == AF_INET) {
if (a->prefixlen > 0 && /* FIXME */ a->gw) {
ifIp4 = ProtocolState::CONFIGURED;
}
} else if (a->addr->sa_family == AF_INET6) {
sockaddr_in6* sin6 = (sockaddr_in6*)a->addr;
auto ip6_addr_data = a->ip6_addr_data;
/* NOTE: we say that IPv6 is configured if there is at least
* one IPv6 address on an interface without scope and in a VALID state,
* which is in fact either PREFERRED or DEPRECATED.
*/
if (sin6->sin6_scope_id == 0 && a->prefixlen > 0 &&
ip6_addr_data && (ip6_addr_data->state & IF_IP6_ADDR_STATE_VALID)) {
ifIp6 = ProtocolState::CONFIGURED;
} else if (sin6->sin6_scope_id != 0 && a->prefixlen > 0 &&
ip6_addr_data && (ip6_addr_data->state & IF_IP6_ADDR_STATE_VALID)) {
// Otherwise report link-local
ifIp6 = ProtocolState::LINKLOCAL;
}
} else {
/* Unknown family */
}
if ((int)ifIp4 > (int)ip4) {
ip4 = ifIp4;
}
if ((int)ifIp6 > (int)ip6) {
ip6 = ifIp6;
}
{
if_t iface;
if (!if_get_by_index(addr->ifindex, &iface)) {
auto state = getInterfaceRuntimeState(iface);
if (state) {
if ((int)ifIp4 > (int)state->ip4State.load()) {
state->ip4State = ifIp4;
}
if ((int)ifIp6 > (int)state->ip6State.load()) {
state->ip6State = ifIp6;
}
}
}
}
}
if_free_if_addrs(addrs);
const auto oldIp4State = ip4State_.load();
const auto oldIp6State = ip6State_.load();
refreshDnsState();
ip4State_ = ip4;
ip6State_ = ip6;
const bool ipConfigured = (ip4 == ProtocolState::CONFIGURED && dns4State_ == DnsState::CONFIGURED) ||
(ip6 == ProtocolState::CONFIGURED && dns6State_ == DnsState::CONFIGURED);
if (state_ == State::IP_CONFIGURED && !ipConfigured) {
transition(State::IFACE_LINK_UP);
} else if (state_ == State::IFACE_LINK_UP && ipConfigured) {
transition(State::IP_CONFIGURED);
} else if (state_ == State::IP_CONFIGURED && ipConfigured) {
/* Transition to the same state here. Once we implement events this will be relevant */
if (ip4 != oldIp4State || ip6 != oldIp6State) {
transition(State::IP_CONFIGURED);
}
}
}
void NetworkManager::refreshDnsState() {
resolv_dns_servers* servers = nullptr;
CHECKV(resolv_get_dns_servers(&servers));
DnsState ip4 = DnsState::UNCONFIGURED;
DnsState ip6 = DnsState::UNCONFIGURED;
for (auto server = servers; server != nullptr; server = server->next) {
if (!server || !server->server) {
continue;
}
if (server->server->sa_family == AF_INET) {
ip4 = DnsState::CONFIGURED;
} else if (server->server->sa_family == AF_INET6) {
ip6 = DnsState::CONFIGURED;
}
}
resolv_free_dns_servers(servers);
dns4State_ = ip4;
dns6State_ = ip6;
}
bool NetworkManager::haveLowerLayerConfiguration(if_t iface) const {
char name[IF_NAMESIZE] = {};
if_get_name(iface, name);
#if HAL_PLATFORM_WIFI && HAL_PLATFORM_NCP
if (!strncmp(name, "wl", 2)) {
auto wifiMan = wifiNetworkManager();
return wifiMan->hasNetworkConfig();
}
#endif // HAL_PLATFORM_WIFI
return true;
}
void NetworkManager::resolvEventHandlerCb(void* arg, const void* data) {
auto self = static_cast<NetworkManager*>(arg);
self->resolvEventHandler(data);
}
void NetworkManager::resolvEventHandler(const void* data) {
refreshIpState();
// NOTE: we could potentially force a cloud ping on DNS change, but
// this seems excessive, and it's better to rely on IP state only instead
// forceCloudPingIfConnected();
}
const char* NetworkManager::stateToName(State state) const {
static const char* const stateNames[] = {
"NONE",
"DISABLED",
"IFACE_DOWN",
"IFACE_REQUEST_DOWN",
"IFACE_REQUEST_UP",
"IFACE_UP",
"IFACE_LINK_UP",
"IP_CONFIGURED"
};
return stateNames[::particle::to_underlying(state)];
}
void NetworkManager::populateInterfaceRuntimeState(bool st) {
for_each_iface([&](if_t iface, unsigned int flags) {
auto state = getInterfaceRuntimeState(iface);
if (!state) {
state = new (std::nothrow) InterfaceRuntimeState;
if (state) {
state->iface = iface;
runState_.pushFront(state);
}
}
if (state) {
state->enabled = st;
if_power_state_t pwr = IF_POWER_STATE_NONE;
if (if_get_power_state(iface, &pwr) != SYSTEM_ERROR_NONE) {
return;
}
if (state->pwrState != pwr) {
state->pwrState = pwr;
uint8_t index;
if_get_index(iface, &index);
LOG(TRACE, "Interface %d power state: %d", index, state->pwrState.load());
}
}
});
}
int NetworkManager::enableInterface(if_t iface) {
// Special case - enable all
if (iface == nullptr) {
populateInterfaceRuntimeState(true);
} else {
auto state = getInterfaceRuntimeState(iface);
CHECK_TRUE(state, SYSTEM_ERROR_NOT_FOUND);
state->enabled = true;
}
return 0;
}
int NetworkManager::disableInterface(if_t iface) {
// Special case - disable all
if (iface == nullptr) {
populateInterfaceRuntimeState(false);
} else {
auto state = getInterfaceRuntimeState(iface);
CHECK_TRUE(state, SYSTEM_ERROR_NOT_FOUND);
state->enabled = false;
}
return 0;
}
int NetworkManager::syncInterfaceStates() {
if (isEstablishingConnections() || isConnectivityAvailable()) {
CHECK(for_each_iface([&](if_t iface, unsigned int flags) {
if (!haveLowerLayerConfiguration(iface)) {
return;
}
auto state = getInterfaceRuntimeState(iface);
if (state) {
if (state->enabled && !(flags & IFF_UP)) {
CHECKV(if_set_flags(iface, IFF_UP));
CHECKV(if_set_xflags(iface, IFXF_DHCP));
} else if (!state->enabled && (flags & IFF_UP)) {
CHECKV(if_clear_flags(iface, IFF_UP));
}
}
}));
}
return 0;
}
NetworkManager::InterfaceRuntimeState* NetworkManager::getInterfaceRuntimeState(if_t iface) const {
for (auto item = runState_.front(); item != nullptr; item = item->next) {
if (item->iface == iface) {
return item;
}
}
return nullptr;
}
bool NetworkManager::isInterfaceEnabled(if_t iface) const {
auto state = getInterfaceRuntimeState(iface);
if (state) {
return state->enabled;
}
return false;
}
int NetworkManager::countEnabledInterfaces() {
int count = 0;
for (auto item = runState_.front(); item != nullptr; item = item->next) {
if (item->enabled && haveLowerLayerConfiguration(item->iface)) {
++count;
}
}
return count;
}
NetworkManager::ProtocolState NetworkManager::getInterfaceIp4State(if_t iface) const {
auto state = getInterfaceRuntimeState(iface);
if (state) {
return state->ip4State;
}
return ProtocolState::UNCONFIGURED;
}
NetworkManager::ProtocolState NetworkManager::getInterfaceIp6State(if_t iface) const {
auto state = getInterfaceRuntimeState(iface);
if (state) {
return state->ip6State;
}
return ProtocolState::UNCONFIGURED;
}
void NetworkManager::resetInterfaceProtocolState(if_t iface) {
for (auto item = runState_.front(); item != nullptr; item = item->next) {
if (!iface || item->iface == iface) {
item->ip4State = ProtocolState::UNCONFIGURED;
item->ip6State = ProtocolState::UNCONFIGURED;
}
}
}
int NetworkManager::powerInterface(if_t iface, bool enable) {
auto ifState = getInterfaceRuntimeState(iface);
if (!ifState) {
LOG(ERROR, "Interface is not populated");
return SYSTEM_ERROR_NOT_FOUND;
}
if_req_power req = {};
if (enable) {
req.state = IF_POWER_STATE_UP;
// Update the interface power here to avoid race condition
// The power state will be updated on NCP power events
// FIXME:
if (ifState->pwrState != IF_POWER_STATE_UP && ifState->pwrState != IF_POWER_STATE_POWERING_UP) {
ifState->pwrState = IF_POWER_STATE_POWERING_UP;
}
LOG(TRACE, "Request to power on the interface");
} else {
req.state = IF_POWER_STATE_DOWN;
// Update the interface power here to avoid race condition
// The power state will be updated on NCP power events
// FIXME:
if (ifState->pwrState != IF_POWER_STATE_DOWN && ifState->pwrState != IF_POWER_STATE_POWERING_DOWN) {
ifState->pwrState = IF_POWER_STATE_POWERING_DOWN;
}
LOG(TRACE, "Request to power off the interface");
}
return if_request(iface, IF_REQ_POWER_STATE, &req, sizeof(req), nullptr);
}
int NetworkManager::waitInterfaceOff(if_t iface, system_tick_t timeout) const {
auto state = getInterfaceRuntimeState(iface);
CHECK_TRUE(state, SYSTEM_ERROR_NOT_FOUND);
if (state->pwrState == IF_POWER_STATE_DOWN) {
return SYSTEM_ERROR_NONE;
}
system_tick_t now = HAL_Timer_Get_Milli_Seconds();
while (state->pwrState != IF_POWER_STATE_DOWN) {
HAL_Delay_Milliseconds(100);
if (HAL_Timer_Get_Milli_Seconds() - now > timeout) {
break;
}
}
if (state->pwrState != IF_POWER_STATE_DOWN) {
return SYSTEM_ERROR_TIMEOUT;
}
return SYSTEM_ERROR_NONE;
}
}} /* namespace particle::system */
#endif /* HAL_PLATFORM_IFAPI */