@@ -22,37 +22,102 @@ using namespace std;
22
22
23
23
OTAUpdate::OTAUpdate () {}
24
24
25
- int OTAUpdate::update (const char * url) {
26
- string res = " " ;
27
- if (modem.write (string (PROMPT (_OTA_RUN)), res, " %s%s\r\n " , CMD_WRITE (_OTA_RUN), url)) {
28
- return 1 ;
29
- }
30
- return 0 ;
31
- }
32
-
33
- int OTAUpdate::setCACert (const char * root_ca) {
34
- string res = " " ;
35
- if (strlen (root_ca) > 0 ) {
36
- modem.write_nowait (string (PROMPT (_OTA_SETCAROOT)),res, " %s%d\r\n " , CMD_WRITE (_OTA_SETCAROOT), strlen (root_ca));
37
- if (modem.passthrough ((uint8_t *)root_ca, strlen (root_ca))) {
38
- return 1 ;
39
- }
40
- }
41
- return 0 ;
42
- }
43
-
44
- bool OTAUpdate::isRunning () {
45
- string res = " " ;
46
- if (modem.write (string (PROMPT (_OTA_RUN)), res, CMD_READ (_OTA_RUN))) {
47
- return atoi (res.c_str ());
48
- }
49
- return 0 ;
50
- }
51
-
52
- int OTAUpdate::getLastError () {
53
- string res = " " ;
54
- if (modem.write (string (PROMPT (_OTA_ERROR)), res, CMD_READ (_OTA_ERROR))) {
55
- return atoi (res.c_str ());
56
- }
57
- return 0 ;
25
+ OTAUpdate::Error OTAUpdate::setCACert (const char * root_ca) {
26
+ string res = " " ;
27
+ if ( root_ca != nullptr && strlen (root_ca) > 0 ) {
28
+ modem.write_nowait (string (PROMPT (_OTA_SETCAROOT)), res, " %s%d\r\n " , CMD_WRITE (_OTA_SETCAROOT), strlen (root_ca));
29
+ if (modem.passthrough ((uint8_t *)root_ca, strlen (root_ca))) {
30
+ return Error::None;
31
+ }
32
+ return Error::Modem;
33
+ }
34
+ return Error::Library;
35
+ }
36
+
37
+ OTAUpdate::Error OTAUpdate::begin () {
38
+ string res = " " ;
39
+ if (modem.write (string (PROMPT (_OTA_BEGIN)), res, " %s" , CMD (_OTA_BEGIN))) {
40
+ return static_cast <OTAUpdate::Error>(atoi (res.c_str ()));
41
+ }
42
+ return Error::Modem;
43
+ }
44
+
45
+ OTAUpdate::Error OTAUpdate::begin (const char * file_path) {
46
+ string res = " " ;
47
+ if ( file_path != nullptr && strlen (file_path) > 0 ) {
48
+ if (modem.write (string (PROMPT (_OTA_BEGIN)), res, " %s%s\r\n " , CMD_WRITE (_OTA_BEGIN), file_path)) {
49
+ return static_cast <OTAUpdate::Error>(atoi (res.c_str ()));
50
+ }
51
+ return Error::Modem;
52
+ }
53
+ return Error::Library;
54
+ }
55
+
56
+ int OTAUpdate::download (const char * url) {
57
+ string res = " " ;
58
+ int ret = -1 ;
59
+ if ( url != nullptr && strlen (url) > 0 ) {
60
+ modem.timeout (EXTENDED_MODEM_TIMEOUT);
61
+ if (modem.write (string (PROMPT (_OTA_DOWNLOAD)), res, " %s%s\r\n " , CMD_WRITE (_OTA_DOWNLOAD), url)) {
62
+ ret = atoi (res.c_str ());
63
+ } else {
64
+ ret = static_cast <int >(Error::Modem);
65
+ }
66
+ } else {
67
+ ret = static_cast <int >(Error::Library);
68
+ }
69
+ modem.timeout (MODEM_TIMEOUT);
70
+ return ret;
71
+ }
72
+
73
+ int OTAUpdate::download (const char * url, const char * file_path) {
74
+ string res = " " ;
75
+ int ret = -1 ;
76
+ if ( url != nullptr && strlen (url) > 0 && file_path != nullptr && strlen (file_path) >0 ) {
77
+ modem.timeout (EXTENDED_MODEM_TIMEOUT);
78
+ if (modem.write (string (PROMPT (_OTA_DOWNLOAD)), res, " %s%s,%s\r\n " , CMD_WRITE (_OTA_DOWNLOAD), url, file_path)) {
79
+ ret = atoi (res.c_str ());
80
+ } else {
81
+ ret = static_cast <int >(Error::Modem);
82
+ }
83
+ } else {
84
+ ret = static_cast <int >(Error::Library);
85
+ }
86
+ modem.timeout (MODEM_TIMEOUT);
87
+ return ret;
88
+ }
89
+
90
+ OTAUpdate::Error OTAUpdate::verify () {
91
+ string res = " " ;
92
+ if (modem.write (string (PROMPT (_OTA_VERIFY)), res, " %s" , CMD (_OTA_VERIFY))) {
93
+ return static_cast <OTAUpdate::Error>(atoi (res.c_str ()));
94
+ }
95
+ return Error::Modem;
96
+ }
97
+
98
+ OTAUpdate::Error OTAUpdate::update () {
99
+ string res = " " ;
100
+ if (modem.write (string (PROMPT (_OTA_UPDATE)), res, " %s" , CMD (_OTA_UPDATE))) {
101
+ return static_cast <OTAUpdate::Error>(atoi (res.c_str ()));
102
+ }
103
+ return Error::Modem;
104
+ }
105
+
106
+ OTAUpdate::Error OTAUpdate::update (const char * file_path) {
107
+ string res = " " ;
108
+ if ( file_path != nullptr && strlen (file_path) > 0 ) {
109
+ if (modem.write (string (PROMPT (_OTA_UPDATE)), res, " %s%s\r\n " , CMD_WRITE (_OTA_UPDATE), file_path)) {
110
+ return Error::None;
111
+ }
112
+ return Error::Modem;
113
+ }
114
+ return Error::Library;
115
+ }
116
+
117
+ OTAUpdate::Error OTAUpdate::reset () {
118
+ string res = " " ;
119
+ if (modem.write (string (PROMPT (_OTA_RESET)), res, " %s" , CMD (_OTA_RESET))) {
120
+ return Error::None;
121
+ }
122
+ return Error::Modem;
58
123
}
0 commit comments