Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of AvnetATT_shape_hackathon by
Wnc.cpp@45:fe90f1fcb4e0, 2016-11-30 (annotated)
- Committer:
 - elmkom
 - Date:
 - Wed Nov 30 14:50:16 2016 +0000
 - Revision:
 - 45:fe90f1fcb4e0
 - Parent:
 - 44:60008ebffdd4
 - Child:
 - 46:733edf15f9e8
 
gsma version
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| elmkom | 42:8500f0cb2ea5 | 1 | #include "Wnc.h" | 
| elmkom | 42:8500f0cb2ea5 | 2 | #include "mbed.h" | 
| elmkom | 42:8500f0cb2ea5 | 3 | #include <cctype> | 
| elmkom | 42:8500f0cb2ea5 | 4 | #include "config_me.h" | 
| elmkom | 42:8500f0cb2ea5 | 5 | #include "SerialBuffered.h" | 
| elmkom | 42:8500f0cb2ea5 | 6 | |
| elmkom | 42:8500f0cb2ea5 | 7 | |
| elmkom | 42:8500f0cb2ea5 | 8 | extern Serial pc; | 
| elmkom | 42:8500f0cb2ea5 | 9 | |
| elmkom | 42:8500f0cb2ea5 | 10 | DigitalOut mdm_uart2_rx_boot_mode_sel(PTC17); // on powerup, 0 = boot mode, 1 = normal boot | 
| elmkom | 42:8500f0cb2ea5 | 11 | DigitalOut mdm_power_on(PTB9); // 0 = turn modem on, 1 = turn modem off (should be held high for >5 seconds to cycle modem) | 
| elmkom | 42:8500f0cb2ea5 | 12 | DigitalInOut mdm_wakeup_in(PTC2); // 0 = let modem sleep, 1 = keep modem awake -- Note: pulled high on shield | 
| elmkom | 42:8500f0cb2ea5 | 13 | |
| elmkom | 42:8500f0cb2ea5 | 14 | DigitalOut mdm_reset(PTC12); // active high | 
| elmkom | 42:8500f0cb2ea5 | 15 | |
| elmkom | 42:8500f0cb2ea5 | 16 | DigitalOut shield_3v3_1v8_sig_trans_ena(PTC4); // 0 = disabled (all signals high impedence, 1 = translation active | 
| elmkom | 42:8500f0cb2ea5 | 17 | DigitalOut mdm_uart1_cts(PTD0); | 
| elmkom | 42:8500f0cb2ea5 | 18 | |
| elmkom | 44:60008ebffdd4 | 19 | DigitalOut uart1Rts(PTD1); | 
| elmkom | 44:60008ebffdd4 | 20 | |
| elmkom | 42:8500f0cb2ea5 | 21 | SerialBuffered mdm(PTD3, PTD2, 512); | 
| elmkom | 42:8500f0cb2ea5 | 22 | |
| elmkom | 44:60008ebffdd4 | 23 | int t3412Timer = 4*60*60; | 
| elmkom | 44:60008ebffdd4 | 24 | int t3324Timer = 20; | 
| elmkom | 42:8500f0cb2ea5 | 25 | bool powerSave = false; | 
| elmkom | 45:fe90f1fcb4e0 | 26 | |
| elmkom | 42:8500f0cb2ea5 | 27 | |
| elmkom | 42:8500f0cb2ea5 | 28 | Wnc::Wnc(void) | 
| elmkom | 42:8500f0cb2ea5 | 29 | { | 
| elmkom | 42:8500f0cb2ea5 | 30 | } | 
| elmkom | 42:8500f0cb2ea5 | 31 | |
| elmkom | 42:8500f0cb2ea5 | 32 | void Wnc::checkPassthrough() | 
| elmkom | 42:8500f0cb2ea5 | 33 | { | 
| elmkom | 42:8500f0cb2ea5 | 34 | if(pc.readable()) | 
| elmkom | 42:8500f0cb2ea5 | 35 | { | 
| elmkom | 42:8500f0cb2ea5 | 36 | if(pc.getc() == '~') | 
| elmkom | 42:8500f0cb2ea5 | 37 | passthrough(); | 
| elmkom | 42:8500f0cb2ea5 | 38 | } | 
| elmkom | 42:8500f0cb2ea5 | 39 | } | 
| elmkom | 42:8500f0cb2ea5 | 40 | |
| elmkom | 42:8500f0cb2ea5 | 41 | |
| elmkom | 42:8500f0cb2ea5 | 42 | void Wnc::passthrough() | 
| elmkom | 42:8500f0cb2ea5 | 43 | { | 
| elmkom | 42:8500f0cb2ea5 | 44 | pc.printf(">>\r\n"); | 
| elmkom | 42:8500f0cb2ea5 | 45 | while(1) | 
| elmkom | 42:8500f0cb2ea5 | 46 | { | 
| elmkom | 42:8500f0cb2ea5 | 47 | char c; | 
| elmkom | 42:8500f0cb2ea5 | 48 | if(pc.readable()) | 
| elmkom | 42:8500f0cb2ea5 | 49 | { | 
| elmkom | 42:8500f0cb2ea5 | 50 | c = pc.getc(); | 
| elmkom | 42:8500f0cb2ea5 | 51 | pc.putc(c); | 
| elmkom | 42:8500f0cb2ea5 | 52 | if(c == '~') | 
| elmkom | 42:8500f0cb2ea5 | 53 | { | 
| elmkom | 42:8500f0cb2ea5 | 54 | pc.printf("exit\r\n"); | 
| elmkom | 42:8500f0cb2ea5 | 55 | break; | 
| elmkom | 42:8500f0cb2ea5 | 56 | } | 
| elmkom | 42:8500f0cb2ea5 | 57 | else if(c == '<') | 
| elmkom | 42:8500f0cb2ea5 | 58 | { | 
| elmkom | 42:8500f0cb2ea5 | 59 | |
| elmkom | 42:8500f0cb2ea5 | 60 | mdm_wakeup_in = 0; | 
| elmkom | 42:8500f0cb2ea5 | 61 | pc.printf("sleep\r\n"); | 
| elmkom | 42:8500f0cb2ea5 | 62 | } | 
| elmkom | 42:8500f0cb2ea5 | 63 | else if(c == '>') | 
| elmkom | 42:8500f0cb2ea5 | 64 | { | 
| elmkom | 42:8500f0cb2ea5 | 65 | mdm_wakeup_in = 1; | 
| elmkom | 42:8500f0cb2ea5 | 66 | pc.printf("wake\r\n"); | 
| elmkom | 42:8500f0cb2ea5 | 67 | } | 
| elmkom | 43:3979ea0a2df3 | 68 | else if(c == '}') | 
| elmkom | 43:3979ea0a2df3 | 69 | { | 
| elmkom | 43:3979ea0a2df3 | 70 | mdm_uart2_rx_boot_mode_sel = 1; | 
| elmkom | 43:3979ea0a2df3 | 71 | pc.printf("rx hi\r\n"); | 
| elmkom | 43:3979ea0a2df3 | 72 | } | 
| elmkom | 43:3979ea0a2df3 | 73 | else if(c == '{') | 
| elmkom | 43:3979ea0a2df3 | 74 | { | 
| elmkom | 43:3979ea0a2df3 | 75 | mdm_uart2_rx_boot_mode_sel = 0; | 
| elmkom | 43:3979ea0a2df3 | 76 | pc.printf("rx low\r\n"); | 
| elmkom | 43:3979ea0a2df3 | 77 | } | 
| elmkom | 43:3979ea0a2df3 | 78 | |
| elmkom | 42:8500f0cb2ea5 | 79 | else if(c == '^') | 
| elmkom | 42:8500f0cb2ea5 | 80 | { | 
| elmkom | 42:8500f0cb2ea5 | 81 | pc.printf("reboot\r\n"); | 
| elmkom | 42:8500f0cb2ea5 | 82 | NVIC_SystemReset(); | 
| elmkom | 42:8500f0cb2ea5 | 83 | } | 
| elmkom | 42:8500f0cb2ea5 | 84 | else | 
| elmkom | 42:8500f0cb2ea5 | 85 | { | 
| elmkom | 42:8500f0cb2ea5 | 86 | |
| elmkom | 42:8500f0cb2ea5 | 87 | mdm.putc(c); | 
| elmkom | 42:8500f0cb2ea5 | 88 | } | 
| elmkom | 42:8500f0cb2ea5 | 89 | } | 
| elmkom | 42:8500f0cb2ea5 | 90 | if(mdm.readable()) | 
| elmkom | 42:8500f0cb2ea5 | 91 | pc.putc(mdm.getc()); | 
| elmkom | 42:8500f0cb2ea5 | 92 | } | 
| elmkom | 42:8500f0cb2ea5 | 93 | } | 
| elmkom | 42:8500f0cb2ea5 | 94 | |
| elmkom | 42:8500f0cb2ea5 | 95 | bool Wnc::isPowerSaveOn() | 
| elmkom | 42:8500f0cb2ea5 | 96 | { | 
| elmkom | 42:8500f0cb2ea5 | 97 | return powerSave; | 
| elmkom | 42:8500f0cb2ea5 | 98 | } | 
| elmkom | 42:8500f0cb2ea5 | 99 | |
| elmkom | 42:8500f0cb2ea5 | 100 | void Wnc::resumePowerSave() | 
| elmkom | 42:8500f0cb2ea5 | 101 | { | 
| elmkom | 42:8500f0cb2ea5 | 102 | mdm_wakeup_in = 0; | 
| elmkom | 42:8500f0cb2ea5 | 103 | } | 
| elmkom | 42:8500f0cb2ea5 | 104 | |
| elmkom | 44:60008ebffdd4 | 105 | int Wnc::gett3412Timer() | 
| elmkom | 44:60008ebffdd4 | 106 | { | 
| elmkom | 44:60008ebffdd4 | 107 | return t3412Timer; | 
| elmkom | 44:60008ebffdd4 | 108 | } | 
| elmkom | 44:60008ebffdd4 | 109 | |
| elmkom | 44:60008ebffdd4 | 110 | int Wnc::gett3324Timer() | 
| elmkom | 44:60008ebffdd4 | 111 | { | 
| elmkom | 44:60008ebffdd4 | 112 | return t3324Timer; | 
| elmkom | 44:60008ebffdd4 | 113 | } | 
| elmkom | 44:60008ebffdd4 | 114 | |
| elmkom | 42:8500f0cb2ea5 | 115 | char* Wnc::read(int timeout_ms) | 
| elmkom | 42:8500f0cb2ea5 | 116 | { | 
| elmkom | 42:8500f0cb2ea5 | 117 | static char response[3200]; | 
| elmkom | 42:8500f0cb2ea5 | 118 | int len = 0; | 
| elmkom | 45:fe90f1fcb4e0 | 119 | char c; | 
| elmkom | 42:8500f0cb2ea5 | 120 | |
| elmkom | 42:8500f0cb2ea5 | 121 | if(timeout_ms > 0) // read until timeout or OK | 
| elmkom | 42:8500f0cb2ea5 | 122 | { | 
| elmkom | 42:8500f0cb2ea5 | 123 | Timer timer; | 
| elmkom | 42:8500f0cb2ea5 | 124 | timer.start(); | 
| elmkom | 42:8500f0cb2ea5 | 125 | while ((len < (3200-1)) && (timer.read_ms() < timeout_ms)) { | 
| elmkom | 42:8500f0cb2ea5 | 126 | if (mdm.readable()) { | 
| elmkom | 45:fe90f1fcb4e0 | 127 | c = mdm.getc(); | 
| elmkom | 45:fe90f1fcb4e0 | 128 | // replace \r\n to make debug messages more compact | 
| elmkom | 45:fe90f1fcb4e0 | 129 | if(c == '\r') c = ' '; | 
| elmkom | 45:fe90f1fcb4e0 | 130 | if(c == '\n') c = ' '; | 
| elmkom | 45:fe90f1fcb4e0 | 131 | |
| elmkom | 45:fe90f1fcb4e0 | 132 | response[len++] = c; | 
| elmkom | 42:8500f0cb2ea5 | 133 | if(len>1 && response[len-2] == 'O' && response[len-1] == 'K') | 
| elmkom | 42:8500f0cb2ea5 | 134 | break; | 
| elmkom | 45:fe90f1fcb4e0 | 135 | |
| elmkom | 42:8500f0cb2ea5 | 136 | } | 
| elmkom | 42:8500f0cb2ea5 | 137 | } | 
| elmkom | 42:8500f0cb2ea5 | 138 | } | 
| elmkom | 42:8500f0cb2ea5 | 139 | |
| elmkom | 42:8500f0cb2ea5 | 140 | response[len] = (char)NULL; | 
| elmkom | 45:fe90f1fcb4e0 | 141 | pc.printf("{%d %s}\r\n",len,response); | 
| elmkom | 42:8500f0cb2ea5 | 142 | return response; | 
| elmkom | 42:8500f0cb2ea5 | 143 | } | 
| elmkom | 42:8500f0cb2ea5 | 144 | |
| elmkom | 42:8500f0cb2ea5 | 145 | char* Wnc::send(const char *cmd, int timeout_ms) | 
| elmkom | 42:8500f0cb2ea5 | 146 | { | 
| elmkom | 45:fe90f1fcb4e0 | 147 | char *reply; | 
| elmkom | 43:3979ea0a2df3 | 148 | |
| elmkom | 43:3979ea0a2df3 | 149 | while (mdm.readable()) { | 
| elmkom | 43:3979ea0a2df3 | 150 | mdm.getc(); | 
| elmkom | 43:3979ea0a2df3 | 151 | } | 
| elmkom | 42:8500f0cb2ea5 | 152 | |
| elmkom | 42:8500f0cb2ea5 | 153 | int tries = 4; | 
| elmkom | 42:8500f0cb2ea5 | 154 | while(tries > 0) | 
| elmkom | 42:8500f0cb2ea5 | 155 | { | 
| elmkom | 42:8500f0cb2ea5 | 156 | tries--; | 
| elmkom | 42:8500f0cb2ea5 | 157 | pc.printf("\r\n<%s>",cmd); | 
| elmkom | 42:8500f0cb2ea5 | 158 | const char *pt = cmd; | 
| elmkom | 42:8500f0cb2ea5 | 159 | size_t n = strlen(cmd); | 
| elmkom | 42:8500f0cb2ea5 | 160 | while (n--) { | 
| elmkom | 42:8500f0cb2ea5 | 161 | mdm.putc(*pt++); | 
| elmkom | 42:8500f0cb2ea5 | 162 | }; | 
| elmkom | 42:8500f0cb2ea5 | 163 | mdm.putc('\r'); | 
| elmkom | 42:8500f0cb2ea5 | 164 | mdm.putc('\n'); | 
| elmkom | 42:8500f0cb2ea5 | 165 | reply = read(timeout_ms); | 
| elmkom | 42:8500f0cb2ea5 | 166 | if(strlen(reply) > 0 && strstr(reply,"OK") !=0) | 
| elmkom | 42:8500f0cb2ea5 | 167 | break; | 
| elmkom | 42:8500f0cb2ea5 | 168 | checkPassthrough(); | 
| elmkom | 42:8500f0cb2ea5 | 169 | } | 
| elmkom | 42:8500f0cb2ea5 | 170 | return reply; | 
| elmkom | 42:8500f0cb2ea5 | 171 | } | 
| elmkom | 42:8500f0cb2ea5 | 172 | |
| elmkom | 42:8500f0cb2ea5 | 173 | bool Wnc::isModemResponding() | 
| elmkom | 42:8500f0cb2ea5 | 174 | { | 
| elmkom | 42:8500f0cb2ea5 | 175 | char *reply = send("AT",WNC_WAIT_TIME_MS); | 
| elmkom | 42:8500f0cb2ea5 | 176 | if(strlen(reply) > 0 && strstr(reply,"OK") !=0) | 
| elmkom | 42:8500f0cb2ea5 | 177 | return true; | 
| elmkom | 42:8500f0cb2ea5 | 178 | return false; | 
| elmkom | 42:8500f0cb2ea5 | 179 | } | 
| elmkom | 42:8500f0cb2ea5 | 180 | |
| elmkom | 42:8500f0cb2ea5 | 181 | void Wnc::setIn() | 
| elmkom | 42:8500f0cb2ea5 | 182 | { | 
| elmkom | 42:8500f0cb2ea5 | 183 | mdm_wakeup_in.input(); | 
| elmkom | 42:8500f0cb2ea5 | 184 | } | 
| elmkom | 42:8500f0cb2ea5 | 185 | |
| elmkom | 42:8500f0cb2ea5 | 186 | void Wnc::toggleWake() | 
| elmkom | 42:8500f0cb2ea5 | 187 | { | 
| elmkom | 42:8500f0cb2ea5 | 188 | mdm_wakeup_in = 0; | 
| elmkom | 42:8500f0cb2ea5 | 189 | wait_ms(2000); | 
| elmkom | 42:8500f0cb2ea5 | 190 | mdm_wakeup_in = 0; | 
| elmkom | 42:8500f0cb2ea5 | 191 | } | 
| elmkom | 42:8500f0cb2ea5 | 192 | |
| elmkom | 45:fe90f1fcb4e0 | 193 | bool Wnc::init(void) { | 
| elmkom | 44:60008ebffdd4 | 194 | uart1Rts = 0; | 
| elmkom | 42:8500f0cb2ea5 | 195 | mdm_wakeup_in.output(); | 
| elmkom | 42:8500f0cb2ea5 | 196 | // disable signal level translator (necessary | 
| elmkom | 42:8500f0cb2ea5 | 197 | // for the modem to boot properly) | 
| elmkom | 42:8500f0cb2ea5 | 198 | shield_3v3_1v8_sig_trans_ena = 0; | 
| elmkom | 42:8500f0cb2ea5 | 199 | |
| elmkom | 42:8500f0cb2ea5 | 200 | // Hard reset the modem (doesn't go through | 
| elmkom | 42:8500f0cb2ea5 | 201 | // the signal level translator) | 
| elmkom | 42:8500f0cb2ea5 | 202 | mdm_reset = 1; | 
| elmkom | 42:8500f0cb2ea5 | 203 | |
| elmkom | 42:8500f0cb2ea5 | 204 | // wait a moment for the modem to react | 
| elmkom | 42:8500f0cb2ea5 | 205 | wait_ms(10); | 
| elmkom | 42:8500f0cb2ea5 | 206 | |
| elmkom | 42:8500f0cb2ea5 | 207 | // Let modem boot | 
| elmkom | 42:8500f0cb2ea5 | 208 | mdm_reset = 0; | 
| elmkom | 42:8500f0cb2ea5 | 209 | |
| elmkom | 42:8500f0cb2ea5 | 210 | // wait a moment for the modem to react | 
| elmkom | 42:8500f0cb2ea5 | 211 | wait(1.0); | 
| elmkom | 42:8500f0cb2ea5 | 212 | |
| elmkom | 42:8500f0cb2ea5 | 213 | // power modem on //off | 
| elmkom | 42:8500f0cb2ea5 | 214 | mdm_power_on = 0; //1; | 
| elmkom | 42:8500f0cb2ea5 | 215 | |
| elmkom | 42:8500f0cb2ea5 | 216 | // insure modem boots into normal operating mode | 
| elmkom | 42:8500f0cb2ea5 | 217 | // and does not go to sleep when powered on | 
| elmkom | 42:8500f0cb2ea5 | 218 | mdm_uart2_rx_boot_mode_sel = 1; | 
| elmkom | 42:8500f0cb2ea5 | 219 | mdm_wakeup_in = 1; | 
| elmkom | 42:8500f0cb2ea5 | 220 | |
| elmkom | 42:8500f0cb2ea5 | 221 | // initialze comm with the modem | 
| elmkom | 42:8500f0cb2ea5 | 222 | mdm.baud(115200); | 
| elmkom | 42:8500f0cb2ea5 | 223 | // clear out potential garbage | 
| elmkom | 42:8500f0cb2ea5 | 224 | while (mdm.readable()) | 
| elmkom | 42:8500f0cb2ea5 | 225 | mdm.getc(); | 
| elmkom | 42:8500f0cb2ea5 | 226 | |
| elmkom | 42:8500f0cb2ea5 | 227 | mdm_uart1_cts = 0; | 
| elmkom | 42:8500f0cb2ea5 | 228 | |
| elmkom | 42:8500f0cb2ea5 | 229 | // wait a moment for the modem to react to signal | 
| elmkom | 42:8500f0cb2ea5 | 230 | // conditions while the level translator is disabled | 
| elmkom | 42:8500f0cb2ea5 | 231 | // (sorry, don't have enough information to know | 
| elmkom | 42:8500f0cb2ea5 | 232 | // what exactly the modem is doing with the current | 
| elmkom | 42:8500f0cb2ea5 | 233 | // pin settings) | 
| elmkom | 42:8500f0cb2ea5 | 234 | wait(1.0); | 
| elmkom | 42:8500f0cb2ea5 | 235 | |
| elmkom | 42:8500f0cb2ea5 | 236 | // enable the signal level translator to start | 
| elmkom | 42:8500f0cb2ea5 | 237 | // modem reset process (modem will be powered down) | 
| elmkom | 42:8500f0cb2ea5 | 238 | shield_3v3_1v8_sig_trans_ena = 1; | 
| elmkom | 42:8500f0cb2ea5 | 239 | |
| elmkom | 42:8500f0cb2ea5 | 240 | // Give the modem 60 secons to start responding by | 
| elmkom | 42:8500f0cb2ea5 | 241 | // sending simple 'AT' commands to modem once per second. | 
| elmkom | 42:8500f0cb2ea5 | 242 | Timer timer; | 
| elmkom | 42:8500f0cb2ea5 | 243 | timer.start(); | 
| elmkom | 42:8500f0cb2ea5 | 244 | while (timer.read() < 60) { | 
| elmkom | 42:8500f0cb2ea5 | 245 | SetLedColor(0x1); //red | 
| elmkom | 42:8500f0cb2ea5 | 246 | if(isModemResponding()) | 
| elmkom | 42:8500f0cb2ea5 | 247 | { | 
| elmkom | 42:8500f0cb2ea5 | 248 | SetLedColor(0); | 
| elmkom | 44:60008ebffdd4 | 249 | //mdm_uart2_rx_boot_mode_sel = 0; | 
| elmkom | 42:8500f0cb2ea5 | 250 | return true; | 
| elmkom | 42:8500f0cb2ea5 | 251 | } | 
| elmkom | 42:8500f0cb2ea5 | 252 | SetLedColor(0); //off | 
| elmkom | 42:8500f0cb2ea5 | 253 | wait_ms(1000 - (timer.read_ms() % 1000)); | 
| elmkom | 42:8500f0cb2ea5 | 254 | pc.printf("\r%d",timer.read_ms()/1000); | 
| elmkom | 42:8500f0cb2ea5 | 255 | |
| elmkom | 42:8500f0cb2ea5 | 256 | } | 
| elmkom | 42:8500f0cb2ea5 | 257 | return false; | 
| elmkom | 42:8500f0cb2ea5 | 258 | } | 
| elmkom | 42:8500f0cb2ea5 | 259 | int Wnc::secToTau(int time) | 
| elmkom | 42:8500f0cb2ea5 | 260 | { | 
| elmkom | 42:8500f0cb2ea5 | 261 | /* | 
| elmkom | 42:8500f0cb2ea5 | 262 | 0 - value is incremented in multiples of 10 minutes | 
| elmkom | 42:8500f0cb2ea5 | 263 | 1 - value is incremented in multiples of 1 hour | 
| elmkom | 42:8500f0cb2ea5 | 264 | 2 - value is incremented in multiples of 10 hours | 
| elmkom | 42:8500f0cb2ea5 | 265 | 3 - value is incremented in multiples of 2 seconds | 
| elmkom | 42:8500f0cb2ea5 | 266 | 4 - value is incremented in multiples of 30 seconds | 
| elmkom | 42:8500f0cb2ea5 | 267 | 5 - value is incremented in multiples of 1 minute | 
| elmkom | 42:8500f0cb2ea5 | 268 | */ | 
| elmkom | 42:8500f0cb2ea5 | 269 | if(time/2 < 32) | 
| elmkom | 42:8500f0cb2ea5 | 270 | { | 
| elmkom | 42:8500f0cb2ea5 | 271 | return (0x3<<5)+time/2; | 
| elmkom | 42:8500f0cb2ea5 | 272 | } | 
| elmkom | 42:8500f0cb2ea5 | 273 | else if(time/30 < 32) | 
| elmkom | 42:8500f0cb2ea5 | 274 | { | 
| elmkom | 42:8500f0cb2ea5 | 275 | return (0x4<<5)+time/30; | 
| elmkom | 42:8500f0cb2ea5 | 276 | } | 
| elmkom | 42:8500f0cb2ea5 | 277 | else if(time/60 < 32) | 
| elmkom | 42:8500f0cb2ea5 | 278 | { | 
| elmkom | 42:8500f0cb2ea5 | 279 | return (0x5<<5)+time/60; | 
| elmkom | 42:8500f0cb2ea5 | 280 | } | 
| elmkom | 42:8500f0cb2ea5 | 281 | else if(time/3600 < 32) | 
| elmkom | 42:8500f0cb2ea5 | 282 | { | 
| elmkom | 42:8500f0cb2ea5 | 283 | return (0x1<<5)+time/3600; | 
| elmkom | 42:8500f0cb2ea5 | 284 | } | 
| elmkom | 42:8500f0cb2ea5 | 285 | else if(time/36000 < 32) | 
| elmkom | 42:8500f0cb2ea5 | 286 | { | 
| elmkom | 42:8500f0cb2ea5 | 287 | return (0x2<<5)+time/36000; | 
| elmkom | 42:8500f0cb2ea5 | 288 | } | 
| elmkom | 42:8500f0cb2ea5 | 289 | else | 
| elmkom | 42:8500f0cb2ea5 | 290 | return (0x7<<5); | 
| elmkom | 42:8500f0cb2ea5 | 291 | |
| elmkom | 42:8500f0cb2ea5 | 292 | |
| elmkom | 42:8500f0cb2ea5 | 293 | } | 
| elmkom | 42:8500f0cb2ea5 | 294 | int Wnc::secToActivity(int time) | 
| elmkom | 42:8500f0cb2ea5 | 295 | { | 
| elmkom | 42:8500f0cb2ea5 | 296 | /* | 
| elmkom | 42:8500f0cb2ea5 | 297 | 0 - value is incremented in multiples of 2 seconds | 
| elmkom | 42:8500f0cb2ea5 | 298 | 1 - value is incremented in multiples of 1 minute | 
| elmkom | 42:8500f0cb2ea5 | 299 | 2 - value is incremented in multiples of decihours | 
| elmkom | 42:8500f0cb2ea5 | 300 | 7 - value indicates that the timer is deactivated. | 
| elmkom | 42:8500f0cb2ea5 | 301 | */ | 
| elmkom | 42:8500f0cb2ea5 | 302 | if(time/2 < 32) | 
| elmkom | 42:8500f0cb2ea5 | 303 | { | 
| elmkom | 42:8500f0cb2ea5 | 304 | return (0x0<<5)+time/2; | 
| elmkom | 42:8500f0cb2ea5 | 305 | } | 
| elmkom | 42:8500f0cb2ea5 | 306 | else if(time/60 < 32) | 
| elmkom | 42:8500f0cb2ea5 | 307 | { | 
| elmkom | 42:8500f0cb2ea5 | 308 | return (0x1<<5)+time/60; | 
| elmkom | 42:8500f0cb2ea5 | 309 | } | 
| elmkom | 42:8500f0cb2ea5 | 310 | else if(time/36000 < 32) | 
| elmkom | 42:8500f0cb2ea5 | 311 | { | 
| elmkom | 42:8500f0cb2ea5 | 312 | return (0x2<<5)+time/36000; | 
| elmkom | 42:8500f0cb2ea5 | 313 | } | 
| elmkom | 42:8500f0cb2ea5 | 314 | else | 
| elmkom | 42:8500f0cb2ea5 | 315 | return (0x7<<5); | 
| elmkom | 42:8500f0cb2ea5 | 316 | |
| elmkom | 42:8500f0cb2ea5 | 317 | } | 
| elmkom | 42:8500f0cb2ea5 | 318 | void Wnc::setPowerSave(bool on,int t3412,int t3324) | 
| elmkom | 42:8500f0cb2ea5 | 319 | { | 
| elmkom | 42:8500f0cb2ea5 | 320 | if(on) | 
| elmkom | 42:8500f0cb2ea5 | 321 | { | 
| elmkom | 44:60008ebffdd4 | 322 | t3412Timer = t3412; | 
| elmkom | 44:60008ebffdd4 | 323 | t3324Timer = t3324; | 
| elmkom | 42:8500f0cb2ea5 | 324 | int tau = secToTau(t3412); | 
| elmkom | 42:8500f0cb2ea5 | 325 | int activity = secToActivity(t3324); | 
| elmkom | 42:8500f0cb2ea5 | 326 | mdm_wakeup_in = 0; //allow power sleep mode | 
| elmkom | 42:8500f0cb2ea5 | 327 | powerSave = true; | 
| elmkom | 42:8500f0cb2ea5 | 328 | char cmd[32]; | 
| elmkom | 45:fe90f1fcb4e0 | 329 | snprintf(cmd,sizeof(cmd),"AT+CPSMS=1,,,%d,%d",tau,activity); | 
| elmkom | 42:8500f0cb2ea5 | 330 | send(cmd, WNC_WAIT_TIME_MS); | 
| elmkom | 42:8500f0cb2ea5 | 331 | } | 
| elmkom | 42:8500f0cb2ea5 | 332 | else | 
| elmkom | 42:8500f0cb2ea5 | 333 | { | 
| elmkom | 42:8500f0cb2ea5 | 334 | mdm_wakeup_in = 1; //disallow power sleep mode | 
| elmkom | 42:8500f0cb2ea5 | 335 | powerSave = false; | 
| elmkom | 42:8500f0cb2ea5 | 336 | send("AT+CPSMS=0", WNC_WAIT_TIME_MS); | 
| elmkom | 42:8500f0cb2ea5 | 337 | } | 
| elmkom | 42:8500f0cb2ea5 | 338 | } | 
| elmkom | 42:8500f0cb2ea5 | 339 | |
| elmkom | 42:8500f0cb2ea5 | 340 | char* Wnc::getIccid() | 
| elmkom | 42:8500f0cb2ea5 | 341 | { | 
| elmkom | 42:8500f0cb2ea5 | 342 | static char iccidBuf[32]; | 
| elmkom | 42:8500f0cb2ea5 | 343 | iccidBuf[0] = NULL; | 
| elmkom | 42:8500f0cb2ea5 | 344 | char* reply = send("AT%CCID",500); | 
| elmkom | 42:8500f0cb2ea5 | 345 | int index = 0; | 
| elmkom | 42:8500f0cb2ea5 | 346 | int begin = -1; | 
| elmkom | 42:8500f0cb2ea5 | 347 | int i = 0; | 
| elmkom | 42:8500f0cb2ea5 | 348 | |
| elmkom | 42:8500f0cb2ea5 | 349 | while (reply[index]) { // While there are more characters to process... | 
| elmkom | 42:8500f0cb2ea5 | 350 | if (begin == -1 && isdigit(reply[index])) { // Upon finding a digit, ... | 
| elmkom | 42:8500f0cb2ea5 | 351 | begin = index; | 
| elmkom | 42:8500f0cb2ea5 | 352 | } | 
| elmkom | 42:8500f0cb2ea5 | 353 | if(begin != -1) | 
| elmkom | 42:8500f0cb2ea5 | 354 | { | 
| elmkom | 42:8500f0cb2ea5 | 355 | if(isdigit(reply[index])) | 
| elmkom | 42:8500f0cb2ea5 | 356 | { | 
| elmkom | 42:8500f0cb2ea5 | 357 | iccidBuf[i++] = reply[index]; | 
| elmkom | 42:8500f0cb2ea5 | 358 | } | 
| elmkom | 42:8500f0cb2ea5 | 359 | else | 
| elmkom | 42:8500f0cb2ea5 | 360 | { | 
| elmkom | 42:8500f0cb2ea5 | 361 | iccidBuf[i++] = NULL; | 
| elmkom | 42:8500f0cb2ea5 | 362 | return iccidBuf; | 
| elmkom | 42:8500f0cb2ea5 | 363 | } | 
| elmkom | 42:8500f0cb2ea5 | 364 | } | 
| elmkom | 42:8500f0cb2ea5 | 365 | index++; | 
| elmkom | 42:8500f0cb2ea5 | 366 | } | 
| elmkom | 42:8500f0cb2ea5 | 367 | return iccidBuf; | 
| elmkom | 42:8500f0cb2ea5 | 368 | } | 
| elmkom | 42:8500f0cb2ea5 | 369 | |
| elmkom | 42:8500f0cb2ea5 | 370 | void Wnc::wakeFromPowerSave() | 
| elmkom | 42:8500f0cb2ea5 | 371 | { | 
| elmkom | 42:8500f0cb2ea5 | 372 | mdm_wakeup_in = 1; | 
| elmkom | 43:3979ea0a2df3 | 373 | pc.printf("wake from power save\r\n"); | 
| elmkom | 42:8500f0cb2ea5 | 374 | } | 
| elmkom | 42:8500f0cb2ea5 | 375 | |
| elmkom | 45:fe90f1fcb4e0 | 376 | bool Wnc::cmdFailed(char* reply,char* msg) | 
| elmkom | 45:fe90f1fcb4e0 | 377 | { | 
| elmkom | 45:fe90f1fcb4e0 | 378 | if(strstr(reply,"OK") > 0) | 
| elmkom | 45:fe90f1fcb4e0 | 379 | return false; | 
| elmkom | 45:fe90f1fcb4e0 | 380 | pc.printf("%s\r\n",msg); | 
| elmkom | 45:fe90f1fcb4e0 | 381 | return true; | 
| elmkom | 45:fe90f1fcb4e0 | 382 | } | 
| elmkom | 45:fe90f1fcb4e0 | 383 | |
| elmkom | 45:fe90f1fcb4e0 | 384 | bool Wnc::startInternet() | 
| elmkom | 42:8500f0cb2ea5 | 385 | { | 
| elmkom | 42:8500f0cb2ea5 | 386 | char *reply; | 
| elmkom | 42:8500f0cb2ea5 | 387 | reply = send("ATE1",WNC_WAIT_TIME_MS); // Echo ON | 
| elmkom | 42:8500f0cb2ea5 | 388 | char apn [32]; | 
| elmkom | 45:fe90f1fcb4e0 | 389 | snprintf(apn,sizeof(apn),"AT%%PDNSET=1,%s,IP",MY_APN_STR); | 
| elmkom | 42:8500f0cb2ea5 | 390 | |
| elmkom | 42:8500f0cb2ea5 | 391 | reply = send(apn, 2*WNC_WAIT_TIME_MS); // Set APN, cmd seems to take a little longer sometimes | 
| elmkom | 45:fe90f1fcb4e0 | 392 | if(cmdFailed(reply,"Failed to set APN")) return false; | 
| elmkom | 45:fe90f1fcb4e0 | 393 | |
| elmkom | 45:fe90f1fcb4e0 | 394 | reply = send("AT%PDNSET?", WNC_WAIT_TIME_MS); | 
| elmkom | 45:fe90f1fcb4e0 | 395 | |
| elmkom | 45:fe90f1fcb4e0 | 396 | // pc.printf("Wiating ...\r\n"); | 
| elmkom | 45:fe90f1fcb4e0 | 397 | // wait(10); | 
| elmkom | 45:fe90f1fcb4e0 | 398 | |
| elmkom | 42:8500f0cb2ea5 | 399 | reply = send("AT@INTERNET=1", WNC_WAIT_TIME_MS); // Internet services enabled | 
| elmkom | 45:fe90f1fcb4e0 | 400 | if(cmdFailed(reply,"Failed to start INTERNET")) | 
| elmkom | 45:fe90f1fcb4e0 | 401 | { | 
| elmkom | 45:fe90f1fcb4e0 | 402 | return false; | 
| elmkom | 45:fe90f1fcb4e0 | 403 | } | 
| elmkom | 42:8500f0cb2ea5 | 404 | reply = send("AT@SOCKDIAL=1", WNC_WAIT_TIME_MS); | 
| elmkom | 45:fe90f1fcb4e0 | 405 | if(cmdFailed(reply,"SOCKDIAL Failed")) | 
| elmkom | 45:fe90f1fcb4e0 | 406 | { | 
| elmkom | 45:fe90f1fcb4e0 | 407 | // passthrough(); | 
| elmkom | 45:fe90f1fcb4e0 | 408 | return false; | 
| elmkom | 45:fe90f1fcb4e0 | 409 | } | 
| elmkom | 45:fe90f1fcb4e0 | 410 | return true; | 
| elmkom | 42:8500f0cb2ea5 | 411 | } | 
| elmkom | 42:8500f0cb2ea5 | 412 | |
| elmkom | 42:8500f0cb2ea5 | 413 | char* Wnc::ping(char* ip) | 
| elmkom | 42:8500f0cb2ea5 | 414 | { | 
| elmkom | 42:8500f0cb2ea5 | 415 | char cmd[32]; | 
| elmkom | 45:fe90f1fcb4e0 | 416 | snprintf(cmd,sizeof(cmd),"AT@PINGREQ=\"%s\"",ip); | 
| elmkom | 42:8500f0cb2ea5 | 417 | return send(cmd,WNC_WAIT_TIME_MS); | 
| elmkom | 42:8500f0cb2ea5 | 418 | } | 
| elmkom | 45:fe90f1fcb4e0 | 419 | |
| elmkom | 42:8500f0cb2ea5 | 420 | bool Wnc::connect(char* ip, int port) | 
| elmkom | 42:8500f0cb2ea5 | 421 | { | 
| elmkom | 42:8500f0cb2ea5 | 422 | char *reply; | 
| elmkom | 42:8500f0cb2ea5 | 423 | |
| elmkom | 45:fe90f1fcb4e0 | 424 | for(int i = 0;i<2;i++) | 
| elmkom | 45:fe90f1fcb4e0 | 425 | { | 
| elmkom | 45:fe90f1fcb4e0 | 426 | if(isModemResponding()) | 
| elmkom | 45:fe90f1fcb4e0 | 427 | break; | 
| elmkom | 45:fe90f1fcb4e0 | 428 | wait(0.5); | 
| elmkom | 45:fe90f1fcb4e0 | 429 | } | 
| elmkom | 45:fe90f1fcb4e0 | 430 | |
| elmkom | 43:3979ea0a2df3 | 431 | if(isModemResponding()) | 
| elmkom | 43:3979ea0a2df3 | 432 | { | 
| elmkom | 43:3979ea0a2df3 | 433 | reply = send("AT@SOCKCREAT=1", WNC_WAIT_TIME_MS); | 
| elmkom | 45:fe90f1fcb4e0 | 434 | if(cmdFailed(reply,"Socket Create Failed")) return false; | 
| elmkom | 45:fe90f1fcb4e0 | 435 | } | 
| elmkom | 45:fe90f1fcb4e0 | 436 | else | 
| elmkom | 45:fe90f1fcb4e0 | 437 | { | 
| elmkom | 45:fe90f1fcb4e0 | 438 | pc.printf("Error Modem not responding\r\n"); | 
| elmkom | 45:fe90f1fcb4e0 | 439 | return false; | 
| elmkom | 43:3979ea0a2df3 | 440 | } | 
| elmkom | 43:3979ea0a2df3 | 441 | |
| elmkom | 43:3979ea0a2df3 | 442 | |
| elmkom | 45:fe90f1fcb4e0 | 443 | char cmd[64]; | 
| elmkom | 45:fe90f1fcb4e0 | 444 | |
| elmkom | 45:fe90f1fcb4e0 | 445 | snprintf(cmd,sizeof(cmd),"AT@SOCKCONN=1,\"%s\",%d",ip,port); | 
| elmkom | 45:fe90f1fcb4e0 | 446 | reply = send(cmd,12*WNC_WAIT_TIME_MS); | 
| elmkom | 45:fe90f1fcb4e0 | 447 | if(cmdFailed(reply,"Socket Connect Failed")) return false; | 
| elmkom | 45:fe90f1fcb4e0 | 448 | |
| elmkom | 45:fe90f1fcb4e0 | 449 | for(int i = 0;i<10;i++) | 
| elmkom | 43:3979ea0a2df3 | 450 | { | 
| elmkom | 45:fe90f1fcb4e0 | 451 | reply = send("AT+CREG?",WNC_WAIT_TIME_MS); | 
| elmkom | 45:fe90f1fcb4e0 | 452 | if(strlen(reply) > 0 && strstr(reply,"2,1") != 0) | 
| elmkom | 43:3979ea0a2df3 | 453 | { | 
| elmkom | 45:fe90f1fcb4e0 | 454 | pc.printf("Connected %s\r\n",reply); | 
| elmkom | 45:fe90f1fcb4e0 | 455 | return true; | 
| elmkom | 43:3979ea0a2df3 | 456 | } | 
| elmkom | 45:fe90f1fcb4e0 | 457 | else | 
| elmkom | 45:fe90f1fcb4e0 | 458 | { | 
| elmkom | 45:fe90f1fcb4e0 | 459 | pc.printf("Unconnected %s\r\n",reply); | 
| elmkom | 45:fe90f1fcb4e0 | 460 | } | 
| elmkom | 45:fe90f1fcb4e0 | 461 | wait(1); | 
| elmkom | 43:3979ea0a2df3 | 462 | } | 
| elmkom | 45:fe90f1fcb4e0 | 463 | return false; | 
| elmkom | 42:8500f0cb2ea5 | 464 | } | 
| elmkom | 42:8500f0cb2ea5 | 465 | |
| elmkom | 42:8500f0cb2ea5 | 466 | void Wnc::disconnect() | 
| elmkom | 42:8500f0cb2ea5 | 467 | { | 
| elmkom | 42:8500f0cb2ea5 | 468 | send("AT@SOCKCLOSE=1", WNC_WAIT_TIME_MS); | 
| elmkom | 42:8500f0cb2ea5 | 469 | } | 
| elmkom | 42:8500f0cb2ea5 | 470 | |
| elmkom | 42:8500f0cb2ea5 | 471 | char* Wnc::encode(int value, char* result, int base) | 
| elmkom | 42:8500f0cb2ea5 | 472 | { | 
| elmkom | 42:8500f0cb2ea5 | 473 | // check that the base if valid | 
| elmkom | 42:8500f0cb2ea5 | 474 | if ( base < 2 || base > 36 ) { | 
| elmkom | 42:8500f0cb2ea5 | 475 | *result = '\0'; | 
| elmkom | 42:8500f0cb2ea5 | 476 | return result; | 
| elmkom | 42:8500f0cb2ea5 | 477 | } | 
| elmkom | 42:8500f0cb2ea5 | 478 | |
| elmkom | 42:8500f0cb2ea5 | 479 | char* ptr = result, *ptr1 = result, tmp_char; | 
| elmkom | 42:8500f0cb2ea5 | 480 | int tmp_value; | 
| elmkom | 42:8500f0cb2ea5 | 481 | |
| elmkom | 42:8500f0cb2ea5 | 482 | do { | 
| elmkom | 42:8500f0cb2ea5 | 483 | tmp_value = value; | 
| elmkom | 42:8500f0cb2ea5 | 484 | value /= base; | 
| elmkom | 42:8500f0cb2ea5 | 485 | *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)]; | 
| elmkom | 42:8500f0cb2ea5 | 486 | } while ( value ); | 
| elmkom | 42:8500f0cb2ea5 | 487 | |
| elmkom | 42:8500f0cb2ea5 | 488 | // Apply negative sign | 
| elmkom | 42:8500f0cb2ea5 | 489 | if ( tmp_value < 0 ) | 
| elmkom | 42:8500f0cb2ea5 | 490 | *ptr++ = '-'; | 
| elmkom | 42:8500f0cb2ea5 | 491 | *ptr-- = '\0'; | 
| elmkom | 42:8500f0cb2ea5 | 492 | |
| elmkom | 42:8500f0cb2ea5 | 493 | while ( ptr1 < ptr ) { | 
| elmkom | 42:8500f0cb2ea5 | 494 | tmp_char = *ptr; | 
| elmkom | 42:8500f0cb2ea5 | 495 | *ptr-- = *ptr1; | 
| elmkom | 42:8500f0cb2ea5 | 496 | *ptr1++ = tmp_char; | 
| elmkom | 42:8500f0cb2ea5 | 497 | } | 
| elmkom | 42:8500f0cb2ea5 | 498 | return result; | 
| elmkom | 42:8500f0cb2ea5 | 499 | } | 
| elmkom | 42:8500f0cb2ea5 | 500 | |
| elmkom | 45:fe90f1fcb4e0 | 501 | bool Wnc::writeSocket(const char * s) | 
| elmkom | 42:8500f0cb2ea5 | 502 | { | 
| elmkom | 42:8500f0cb2ea5 | 503 | |
| elmkom | 42:8500f0cb2ea5 | 504 | char num2str[6]; | 
| elmkom | 42:8500f0cb2ea5 | 505 | size_t sLen = strlen(s); | 
| elmkom | 42:8500f0cb2ea5 | 506 | if (sLen <= 99999) | 
| elmkom | 42:8500f0cb2ea5 | 507 | { | 
| elmkom | 45:fe90f1fcb4e0 | 508 | char cmd[sLen*2+32]; | 
| elmkom | 45:fe90f1fcb4e0 | 509 | int index = snprintf(cmd,sizeof(cmd),"AT@SOCKWRITE=1,%d,\"",sLen); | 
| elmkom | 42:8500f0cb2ea5 | 510 | |
| elmkom | 42:8500f0cb2ea5 | 511 | while(*s != '\0') | 
| elmkom | 42:8500f0cb2ea5 | 512 | { | 
| elmkom | 42:8500f0cb2ea5 | 513 | encode((int)*s++, num2str, 16); | 
| elmkom | 42:8500f0cb2ea5 | 514 | // Always 2-digit ascii hex: | 
| elmkom | 42:8500f0cb2ea5 | 515 | if (strlen(num2str) == 1) | 
| elmkom | 42:8500f0cb2ea5 | 516 | { | 
| elmkom | 42:8500f0cb2ea5 | 517 | num2str[2] = '\0'; | 
| elmkom | 42:8500f0cb2ea5 | 518 | num2str[1] = num2str[0]; | 
| elmkom | 42:8500f0cb2ea5 | 519 | num2str[0] = '0'; | 
| elmkom | 42:8500f0cb2ea5 | 520 | } | 
| elmkom | 45:fe90f1fcb4e0 | 521 | cmd[index++] = num2str[0]; | 
| elmkom | 45:fe90f1fcb4e0 | 522 | cmd[index++] = num2str[1]; | 
| elmkom | 42:8500f0cb2ea5 | 523 | } | 
| elmkom | 45:fe90f1fcb4e0 | 524 | cmd[index++] = '"'; | 
| elmkom | 45:fe90f1fcb4e0 | 525 | cmd[index] = '\0'; | 
| elmkom | 45:fe90f1fcb4e0 | 526 | char* reply = send(cmd, WNC_WAIT_TIME_MS); | 
| elmkom | 45:fe90f1fcb4e0 | 527 | if(cmdFailed(reply,"Send Failed")) return false; | 
| elmkom | 45:fe90f1fcb4e0 | 528 | return true; | 
| elmkom | 42:8500f0cb2ea5 | 529 | } | 
| elmkom | 42:8500f0cb2ea5 | 530 | else | 
| elmkom | 45:fe90f1fcb4e0 | 531 | { | 
| elmkom | 42:8500f0cb2ea5 | 532 | pc.puts("sockwrite Err, string to long\r\n"); | 
| elmkom | 45:fe90f1fcb4e0 | 533 | return false; | 
| elmkom | 45:fe90f1fcb4e0 | 534 | } | 
| elmkom | 45:fe90f1fcb4e0 | 535 | |
| elmkom | 42:8500f0cb2ea5 | 536 | } | 
| elmkom | 42:8500f0cb2ea5 | 537 | |
| elmkom | 45:fe90f1fcb4e0 | 538 | int Wnc::hex_to_int(char A) | 
| elmkom | 45:fe90f1fcb4e0 | 539 | { | 
| elmkom | 45:fe90f1fcb4e0 | 540 | return (A > '9')? (A &~ 0x20) - 'A' + 10: (A - '0'); | 
| elmkom | 42:8500f0cb2ea5 | 541 | } | 
| elmkom | 42:8500f0cb2ea5 | 542 | |
| elmkom | 45:fe90f1fcb4e0 | 543 | int Wnc::hex_to_ascii(char h, char l) | 
| elmkom | 45:fe90f1fcb4e0 | 544 | { | 
| elmkom | 45:fe90f1fcb4e0 | 545 | return hex_to_int(h) * 16 + hex_to_int(l); | 
| elmkom | 42:8500f0cb2ea5 | 546 | } | 
| elmkom | 42:8500f0cb2ea5 | 547 | |
| elmkom | 42:8500f0cb2ea5 | 548 | int Wnc::indexOf(char* str, char c) | 
| elmkom | 42:8500f0cb2ea5 | 549 | { | 
| elmkom | 42:8500f0cb2ea5 | 550 | int index = 0; | 
| elmkom | 42:8500f0cb2ea5 | 551 | while(str[index] != 0) | 
| elmkom | 42:8500f0cb2ea5 | 552 | { | 
| elmkom | 42:8500f0cb2ea5 | 553 | if(str[index] == c) | 
| elmkom | 42:8500f0cb2ea5 | 554 | return index; | 
| elmkom | 42:8500f0cb2ea5 | 555 | index++; | 
| elmkom | 42:8500f0cb2ea5 | 556 | } | 
| elmkom | 42:8500f0cb2ea5 | 557 | return -1; | 
| elmkom | 42:8500f0cb2ea5 | 558 | } | 
| elmkom | 42:8500f0cb2ea5 | 559 | |
| elmkom | 42:8500f0cb2ea5 | 560 | char* Wnc::readSocket() | 
| elmkom | 42:8500f0cb2ea5 | 561 | { | 
| elmkom | 42:8500f0cb2ea5 | 562 | |
| elmkom | 42:8500f0cb2ea5 | 563 | static char data[1000]; | 
| elmkom | 42:8500f0cb2ea5 | 564 | int i = 0; | 
| elmkom | 42:8500f0cb2ea5 | 565 | char *reply; | 
| elmkom | 42:8500f0cb2ea5 | 566 | reply = send("AT@SOCKREAD=1,1024",1000); | 
| elmkom | 42:8500f0cb2ea5 | 567 | if(strlen(reply) > 0) | 
| elmkom | 42:8500f0cb2ea5 | 568 | { | 
| elmkom | 42:8500f0cb2ea5 | 569 | int pos_start = indexOf(reply,'"'); | 
| elmkom | 42:8500f0cb2ea5 | 570 | |
| elmkom | 42:8500f0cb2ea5 | 571 | if(pos_start > 0) | 
| elmkom | 42:8500f0cb2ea5 | 572 | { | 
| elmkom | 42:8500f0cb2ea5 | 573 | pos_start+=1; | 
| elmkom | 42:8500f0cb2ea5 | 574 | int length = indexOf(&reply[pos_start],'"'); | 
| elmkom | 42:8500f0cb2ea5 | 575 | |
| elmkom | 42:8500f0cb2ea5 | 576 | if(length > 0) | 
| elmkom | 42:8500f0cb2ea5 | 577 | { | 
| elmkom | 42:8500f0cb2ea5 | 578 | char hi; | 
| elmkom | 42:8500f0cb2ea5 | 579 | char low; | 
| elmkom | 42:8500f0cb2ea5 | 580 | for(i = 0; i < length; i++){ | 
| elmkom | 42:8500f0cb2ea5 | 581 | if(i % 2 != 0){ | 
| elmkom | 42:8500f0cb2ea5 | 582 | low = reply[pos_start++]; | 
| elmkom | 42:8500f0cb2ea5 | 583 | data[i/2] = (char) hex_to_ascii(hi,low); | 
| elmkom | 42:8500f0cb2ea5 | 584 | }else{ | 
| elmkom | 42:8500f0cb2ea5 | 585 | hi = reply[pos_start++]; | 
| elmkom | 42:8500f0cb2ea5 | 586 | } | 
| elmkom | 42:8500f0cb2ea5 | 587 | } | 
| elmkom | 44:60008ebffdd4 | 588 | data[length/2] = NULL; | 
| elmkom | 42:8500f0cb2ea5 | 589 | } | 
| elmkom | 42:8500f0cb2ea5 | 590 | } | 
| elmkom | 42:8500f0cb2ea5 | 591 | } | 
| elmkom | 42:8500f0cb2ea5 | 592 | data[i] = NULL; | 
| elmkom | 42:8500f0cb2ea5 | 593 | return data; | 
| elmkom | 45:fe90f1fcb4e0 | 594 | } | 
| elmkom | 45:fe90f1fcb4e0 | 595 | |
| elmkom | 45:fe90f1fcb4e0 | 596 | char* Wnc::resolveDn(const char* name) | 
| elmkom | 45:fe90f1fcb4e0 | 597 | { | 
| elmkom | 45:fe90f1fcb4e0 | 598 | char cmd[64]; | 
| elmkom | 45:fe90f1fcb4e0 | 599 | |
| elmkom | 45:fe90f1fcb4e0 | 600 | snprintf(cmd,sizeof(cmd),"AT@DNSRESVDON=\"%s\"",name); | 
| elmkom | 45:fe90f1fcb4e0 | 601 | char* reply = send(cmd,12*WNC_WAIT_TIME_MS); | 
| elmkom | 45:fe90f1fcb4e0 | 602 | static char ipBuf[32]; | 
| elmkom | 45:fe90f1fcb4e0 | 603 | ipBuf[0] = NULL; | 
| elmkom | 45:fe90f1fcb4e0 | 604 | int index = 0; | 
| elmkom | 45:fe90f1fcb4e0 | 605 | int begin = -1; | 
| elmkom | 45:fe90f1fcb4e0 | 606 | int i = 0; | 
| elmkom | 45:fe90f1fcb4e0 | 607 | |
| elmkom | 45:fe90f1fcb4e0 | 608 | while (reply[index]) { // While there are more characters to process... | 
| elmkom | 45:fe90f1fcb4e0 | 609 | if (begin == -1 && isdigit(reply[index])) { // Upon finding a digit, ... | 
| elmkom | 45:fe90f1fcb4e0 | 610 | begin = index; | 
| elmkom | 45:fe90f1fcb4e0 | 611 | } | 
| elmkom | 45:fe90f1fcb4e0 | 612 | if(begin != -1) | 
| elmkom | 45:fe90f1fcb4e0 | 613 | { | 
| elmkom | 45:fe90f1fcb4e0 | 614 | if(isdigit(reply[index]) || reply[index] == '.') | 
| elmkom | 45:fe90f1fcb4e0 | 615 | { | 
| elmkom | 45:fe90f1fcb4e0 | 616 | ipBuf[i++] = reply[index]; | 
| elmkom | 45:fe90f1fcb4e0 | 617 | } | 
| elmkom | 45:fe90f1fcb4e0 | 618 | else | 
| elmkom | 45:fe90f1fcb4e0 | 619 | { | 
| elmkom | 45:fe90f1fcb4e0 | 620 | ipBuf[i++] = NULL; | 
| elmkom | 45:fe90f1fcb4e0 | 621 | return ipBuf; | 
| elmkom | 45:fe90f1fcb4e0 | 622 | } | 
| elmkom | 45:fe90f1fcb4e0 | 623 | } | 
| elmkom | 45:fe90f1fcb4e0 | 624 | index++; | 
| elmkom | 45:fe90f1fcb4e0 | 625 | } | 
| elmkom | 45:fe90f1fcb4e0 | 626 | return ipBuf; | 
| elmkom | 42:8500f0cb2ea5 | 627 | } | 
