![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
WIZnet-IoTShield-AMM592
main.cpp@1:8b209e861bb1, 2019-03-20 (annotated)
- Committer:
- hkjung
- Date:
- Wed Mar 20 05:32:43 2019 +0000
- Revision:
- 1:8b209e861bb1
- Parent:
- 0:5cd6077e0a8e
- Child:
- 2:957b12467039
Added license identifier and some comment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hkjung | 0:5cd6077e0a8e | 1 | /* WIZnet IoT Shield Cat.M1 Sample code for Arm MBED |
hkjung | 0:5cd6077e0a8e | 2 | * Copyright (c) 2019 WIZnet Co., Ltd. |
hkjung | 0:5cd6077e0a8e | 3 | * SPDX-License-Identifier: Apache-2.0 |
hkjung | 0:5cd6077e0a8e | 4 | */ |
hkjung | 0:5cd6077e0a8e | 5 | |
hkjung | 0:5cd6077e0a8e | 6 | /* |
hkjung | 0:5cd6077e0a8e | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
hkjung | 0:5cd6077e0a8e | 8 | * you may not use this file except in compliance with the License. |
hkjung | 0:5cd6077e0a8e | 9 | * You may obtain a copy of the License at |
hkjung | 0:5cd6077e0a8e | 10 | * |
hkjung | 0:5cd6077e0a8e | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
hkjung | 0:5cd6077e0a8e | 12 | * |
hkjung | 0:5cd6077e0a8e | 13 | * Unless required by applicable law or agreed to in writing, software |
hkjung | 0:5cd6077e0a8e | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
hkjung | 0:5cd6077e0a8e | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
hkjung | 0:5cd6077e0a8e | 16 | * |
hkjung | 0:5cd6077e0a8e | 17 | * See the License for the specific language governing permissions and |
hkjung | 0:5cd6077e0a8e | 18 | * limitations under the License. |
hkjung | 0:5cd6077e0a8e | 19 | * |
hkjung | 0:5cd6077e0a8e | 20 | */ |
hkjung | 0:5cd6077e0a8e | 21 | |
hkjung | 0:5cd6077e0a8e | 22 | |
hkjung | 0:5cd6077e0a8e | 23 | #include <string> |
hkjung | 0:5cd6077e0a8e | 24 | #include "mbed.h" |
hkjung | 0:5cd6077e0a8e | 25 | |
hkjung | 0:5cd6077e0a8e | 26 | #define RET_OK 1 |
hkjung | 0:5cd6077e0a8e | 27 | #define RET_NOK -1 |
hkjung | 0:5cd6077e0a8e | 28 | #define DEBUG_ENABLE 1 |
hkjung | 0:5cd6077e0a8e | 29 | #define DEBUG_DISABLE 0 |
hkjung | 0:5cd6077e0a8e | 30 | #define ON 1 |
hkjung | 0:5cd6077e0a8e | 31 | #define OFF 0 |
hkjung | 0:5cd6077e0a8e | 32 | |
hkjung | 0:5cd6077e0a8e | 33 | #define MAX_BUF_SIZE 1024 |
hkjung | 0:5cd6077e0a8e | 34 | |
hkjung | 0:5cd6077e0a8e | 35 | #define BG96_APN_PROTOCOL_IPv4 1 |
hkjung | 0:5cd6077e0a8e | 36 | #define BG96_APN_PROTOCOL_IPv6 2 |
hkjung | 0:5cd6077e0a8e | 37 | #define BG96_DEFAULT_TIMEOUT 1000 |
hkjung | 0:5cd6077e0a8e | 38 | #define BG96_CONNECT_TIMEOUT 15000 |
hkjung | 0:5cd6077e0a8e | 39 | #define BG96_SEND_TIMEOUT 500 |
hkjung | 0:5cd6077e0a8e | 40 | #define BG96_RECV_TIMEOUT 500 |
hkjung | 0:5cd6077e0a8e | 41 | |
hkjung | 0:5cd6077e0a8e | 42 | #define BG96_APN_PROTOCOL BG96_APN_PROTOCOL_IPv6 |
hkjung | 0:5cd6077e0a8e | 43 | #define BG96_DEFAULT_BAUD_RATE 115200 |
hkjung | 0:5cd6077e0a8e | 44 | #define BG96_PARSER_DELIMITER "\r\n" |
hkjung | 0:5cd6077e0a8e | 45 | |
hkjung | 0:5cd6077e0a8e | 46 | #define CATM1_APN_SKT "lte-internet.sktelecom.com" |
hkjung | 0:5cd6077e0a8e | 47 | |
hkjung | 0:5cd6077e0a8e | 48 | #define CATM1_DEVICE_NAME_BG96 "BG96" |
hkjung | 0:5cd6077e0a8e | 49 | #define DEVNAME CATM1_DEVICE_NAME_BG96 |
hkjung | 0:5cd6077e0a8e | 50 | |
hkjung | 0:5cd6077e0a8e | 51 | #define devlog(f_, ...) if(CATM1_DEVICE_DEBUG == DEBUG_ENABLE) { pc.printf("\r\n[%s] ", DEVNAME); pc.printf((f_), ##__VA_ARGS__); } |
hkjung | 0:5cd6077e0a8e | 52 | #define myprintf(f_, ...) {pc.printf("\r\n[MAIN] "); pc.printf((f_), ##__VA_ARGS__);} |
hkjung | 0:5cd6077e0a8e | 53 | |
hkjung | 0:5cd6077e0a8e | 54 | /* Pin configuraiton */ |
hkjung | 0:5cd6077e0a8e | 55 | // Cat.M1 |
hkjung | 0:5cd6077e0a8e | 56 | #define MBED_CONF_IOTSHIELD_CATM1_TX D8 |
hkjung | 0:5cd6077e0a8e | 57 | #define MBED_CONF_IOTSHIELD_CATM1_RX D2 |
hkjung | 1:8b209e861bb1 | 58 | #define MBED_CONF_IOTSHIELD_CATM1_RESET D7 |
hkjung | 0:5cd6077e0a8e | 59 | #define MBED_CONF_IOTSHIELD_CATM1_PWRKEY D9 |
hkjung | 1:8b209e861bb1 | 60 | |
hkjung | 0:5cd6077e0a8e | 61 | // Sensors |
hkjung | 0:5cd6077e0a8e | 62 | #define MBED_CONF_IOTSHIELD_SENSOR_CDS A0 |
hkjung | 0:5cd6077e0a8e | 63 | #define MBED_CONF_IOTSHIELD_SENSOR_TEMP A1 |
hkjung | 0:5cd6077e0a8e | 64 | |
hkjung | 0:5cd6077e0a8e | 65 | /* Debug message settings */ |
hkjung | 0:5cd6077e0a8e | 66 | #define BG96_PARSER_DEBUG DEBUG_DISABLE |
hkjung | 0:5cd6077e0a8e | 67 | #define CATM1_DEVICE_DEBUG DEBUG_ENABLE |
hkjung | 0:5cd6077e0a8e | 68 | |
hkjung | 0:5cd6077e0a8e | 69 | // Functions: Module Status |
hkjung | 0:5cd6077e0a8e | 70 | void waitCatM1Ready(void); |
hkjung | 0:5cd6077e0a8e | 71 | int8_t setEchoStatus_BG96(bool onoff); |
hkjung | 0:5cd6077e0a8e | 72 | int8_t getUsimStatus_BG96(void); |
hkjung | 0:5cd6077e0a8e | 73 | int8_t getNetworkStatus_BG96(void); |
hkjung | 0:5cd6077e0a8e | 74 | int8_t checknSetApn_BG96(const char * apn); |
hkjung | 0:5cd6077e0a8e | 75 | int8_t getFirmwareVersion_BG96(char * version); |
hkjung | 0:5cd6077e0a8e | 76 | |
hkjung | 0:5cd6077e0a8e | 77 | // Functions: PDP context |
hkjung | 0:5cd6077e0a8e | 78 | int8_t setContextActivate_BG96(void); // Activate a PDP Context |
hkjung | 0:5cd6077e0a8e | 79 | int8_t setContextDeactivate_BG96(void); // Deactivate a PDP Context |
hkjung | 1:8b209e861bb1 | 80 | int8_t getIpAddress_BG96(char * ipstr); |
hkjung | 0:5cd6077e0a8e | 81 | |
hkjung | 0:5cd6077e0a8e | 82 | // Functions: Ping test |
hkjung | 0:5cd6077e0a8e | 83 | void pingToHost_BG96(char * host, int timeout, int pingnum); |
hkjung | 0:5cd6077e0a8e | 84 | |
hkjung | 0:5cd6077e0a8e | 85 | |
hkjung | 0:5cd6077e0a8e | 86 | Serial pc(USBTX, USBRX); // USB debug |
hkjung | 0:5cd6077e0a8e | 87 | |
hkjung | 0:5cd6077e0a8e | 88 | UARTSerial *_serial; // Cat.M1 module |
hkjung | 0:5cd6077e0a8e | 89 | ATCmdParser *_parser; |
hkjung | 0:5cd6077e0a8e | 90 | |
hkjung | 1:8b209e861bb1 | 91 | DigitalOut _RESET_BG96(MBED_CONF_IOTSHIELD_CATM1_RESET); |
hkjung | 1:8b209e861bb1 | 92 | DigitalOut _PWRKEY_BG96(MBED_CONF_IOTSHIELD_CATM1_PWRKEY); |
hkjung | 0:5cd6077e0a8e | 93 | |
hkjung | 0:5cd6077e0a8e | 94 | void serialPcInit(void) |
hkjung | 0:5cd6077e0a8e | 95 | { |
hkjung | 0:5cd6077e0a8e | 96 | pc.baud(115200); |
hkjung | 0:5cd6077e0a8e | 97 | pc.format(8, Serial::None, 1); |
hkjung | 0:5cd6077e0a8e | 98 | } |
hkjung | 0:5cd6077e0a8e | 99 | |
hkjung | 0:5cd6077e0a8e | 100 | void serialDeviceInit(PinName tx, PinName rx, int baudrate) |
hkjung | 0:5cd6077e0a8e | 101 | { |
hkjung | 0:5cd6077e0a8e | 102 | _serial = new UARTSerial(tx, rx, baudrate); |
hkjung | 0:5cd6077e0a8e | 103 | } |
hkjung | 0:5cd6077e0a8e | 104 | |
hkjung | 0:5cd6077e0a8e | 105 | void serialAtParserInit(const char *delimiter, bool debug_en) |
hkjung | 0:5cd6077e0a8e | 106 | { |
hkjung | 0:5cd6077e0a8e | 107 | _parser = new ATCmdParser(_serial); |
hkjung | 0:5cd6077e0a8e | 108 | _parser->debug_on(debug_en); |
hkjung | 0:5cd6077e0a8e | 109 | _parser->set_delimiter(delimiter); |
hkjung | 0:5cd6077e0a8e | 110 | _parser->set_timeout(BG96_DEFAULT_TIMEOUT); |
hkjung | 0:5cd6077e0a8e | 111 | } |
hkjung | 0:5cd6077e0a8e | 112 | |
hkjung | 0:5cd6077e0a8e | 113 | void catm1DeviceInit(void) |
hkjung | 0:5cd6077e0a8e | 114 | { |
hkjung | 0:5cd6077e0a8e | 115 | serialDeviceInit( MBED_CONF_IOTSHIELD_CATM1_TX, |
hkjung | 0:5cd6077e0a8e | 116 | MBED_CONF_IOTSHIELD_CATM1_RX, |
hkjung | 0:5cd6077e0a8e | 117 | BG96_DEFAULT_BAUD_RATE); |
hkjung | 0:5cd6077e0a8e | 118 | |
hkjung | 0:5cd6077e0a8e | 119 | serialAtParserInit( BG96_PARSER_DELIMITER, |
hkjung | 0:5cd6077e0a8e | 120 | BG96_PARSER_DEBUG); |
hkjung | 0:5cd6077e0a8e | 121 | } |
hkjung | 0:5cd6077e0a8e | 122 | |
hkjung | 1:8b209e861bb1 | 123 | void catm1DeviceReset_BG96(void) |
hkjung | 1:8b209e861bb1 | 124 | { |
hkjung | 1:8b209e861bb1 | 125 | _RESET_BG96 = 1; |
hkjung | 1:8b209e861bb1 | 126 | _PWRKEY_BG96 = 1; |
hkjung | 1:8b209e861bb1 | 127 | wait_ms(300); |
hkjung | 0:5cd6077e0a8e | 128 | |
hkjung | 1:8b209e861bb1 | 129 | _RESET_BG96 = 0; |
hkjung | 1:8b209e861bb1 | 130 | _PWRKEY_BG96 = 0; |
hkjung | 1:8b209e861bb1 | 131 | wait_ms(400); |
hkjung | 0:5cd6077e0a8e | 132 | |
hkjung | 1:8b209e861bb1 | 133 | _RESET_BG96 = 1; |
hkjung | 1:8b209e861bb1 | 134 | wait_ms(1000); |
hkjung | 0:5cd6077e0a8e | 135 | } |
hkjung | 0:5cd6077e0a8e | 136 | |
hkjung | 1:8b209e861bb1 | 137 | // ---------------------------------------------------------------- |
hkjung | 1:8b209e861bb1 | 138 | // Main routine |
hkjung | 1:8b209e861bb1 | 139 | // ---------------------------------------------------------------- |
hkjung | 1:8b209e861bb1 | 140 | |
hkjung | 0:5cd6077e0a8e | 141 | int main() |
hkjung | 0:5cd6077e0a8e | 142 | { |
hkjung | 0:5cd6077e0a8e | 143 | serialPcInit(); |
hkjung | 0:5cd6077e0a8e | 144 | catm1DeviceInit(); |
hkjung | 0:5cd6077e0a8e | 145 | |
hkjung | 0:5cd6077e0a8e | 146 | myprintf("Waiting for Cat.M1 Module Ready...\r\n"); |
hkjung | 0:5cd6077e0a8e | 147 | |
hkjung | 1:8b209e861bb1 | 148 | catm1DeviceReset_BG96(); |
hkjung | 0:5cd6077e0a8e | 149 | |
hkjung | 0:5cd6077e0a8e | 150 | waitCatM1Ready(); |
hkjung | 1:8b209e861bb1 | 151 | |
hkjung | 1:8b209e861bb1 | 152 | wait_ms(5000); |
hkjung | 0:5cd6077e0a8e | 153 | |
hkjung | 0:5cd6077e0a8e | 154 | myprintf("System Init Complete\r\n"); |
hkjung | 0:5cd6077e0a8e | 155 | |
hkjung | 0:5cd6077e0a8e | 156 | myprintf("WIZnet IoT Shield for Arm MBED"); |
hkjung | 0:5cd6077e0a8e | 157 | myprintf("LTE Cat.M1 Version"); |
hkjung | 0:5cd6077e0a8e | 158 | myprintf("================================================="); |
hkjung | 0:5cd6077e0a8e | 159 | myprintf(">> Target Board: WIoT-QC01 (Quectel BG96)"); |
hkjung | 0:5cd6077e0a8e | 160 | myprintf(">> Sample Code: Ping Test"); |
hkjung | 0:5cd6077e0a8e | 161 | myprintf("=================================================\r\n"); |
hkjung | 0:5cd6077e0a8e | 162 | |
hkjung | 0:5cd6077e0a8e | 163 | setEchoStatus_BG96(OFF); |
hkjung | 0:5cd6077e0a8e | 164 | |
hkjung | 0:5cd6077e0a8e | 165 | getUsimStatus_BG96(); |
hkjung | 0:5cd6077e0a8e | 166 | |
hkjung | 0:5cd6077e0a8e | 167 | getNetworkStatus_BG96(); |
hkjung | 0:5cd6077e0a8e | 168 | |
hkjung | 0:5cd6077e0a8e | 169 | checknSetApn_BG96(CATM1_APN_SKT); |
hkjung | 0:5cd6077e0a8e | 170 | |
hkjung | 0:5cd6077e0a8e | 171 | char ping_dest_1st[] = "8.8.8.8"; |
hkjung | 0:5cd6077e0a8e | 172 | char ping_dest_2nd[] = "www.google.com"; |
hkjung | 0:5cd6077e0a8e | 173 | |
hkjung | 0:5cd6077e0a8e | 174 | setContextActivate_BG96(); |
hkjung | 0:5cd6077e0a8e | 175 | |
hkjung | 0:5cd6077e0a8e | 176 | myprintf("[Ping] Host: %s\r\n", ping_dest_1st); |
hkjung | 0:5cd6077e0a8e | 177 | printPingToHost_BG96(ping_dest_1st, 2, 4); |
hkjung | 0:5cd6077e0a8e | 178 | |
hkjung | 0:5cd6077e0a8e | 179 | wait_ms(2000); |
hkjung | 0:5cd6077e0a8e | 180 | |
hkjung | 0:5cd6077e0a8e | 181 | myprintf("[Ping] Host: %s\r\n", ping_dest_2nd); |
hkjung | 0:5cd6077e0a8e | 182 | printPingToHost_BG96(ping_dest_2nd, 2, 4); |
hkjung | 0:5cd6077e0a8e | 183 | |
hkjung | 0:5cd6077e0a8e | 184 | setContextDeactivate_BG96(); |
hkjung | 0:5cd6077e0a8e | 185 | } |
hkjung | 0:5cd6077e0a8e | 186 | |
hkjung | 1:8b209e861bb1 | 187 | // ---------------------------------------------------------------- |
hkjung | 1:8b209e861bb1 | 188 | // Functions: Cat.M1 Status |
hkjung | 1:8b209e861bb1 | 189 | // ---------------------------------------------------------------- |
hkjung | 1:8b209e861bb1 | 190 | |
hkjung | 0:5cd6077e0a8e | 191 | void waitCatM1Ready(void) |
hkjung | 0:5cd6077e0a8e | 192 | { |
hkjung | 0:5cd6077e0a8e | 193 | while(1) |
hkjung | 0:5cd6077e0a8e | 194 | { |
hkjung | 0:5cd6077e0a8e | 195 | if(_parser->recv("RDY")) |
hkjung | 0:5cd6077e0a8e | 196 | { |
hkjung | 0:5cd6077e0a8e | 197 | myprintf("BG96 ready\r\n"); |
hkjung | 0:5cd6077e0a8e | 198 | return ; |
hkjung | 0:5cd6077e0a8e | 199 | } |
hkjung | 0:5cd6077e0a8e | 200 | else if(_parser->send("AT") && _parser->recv("OK")) |
hkjung | 0:5cd6077e0a8e | 201 | { |
hkjung | 0:5cd6077e0a8e | 202 | myprintf("BG96 already available\r\n"); |
hkjung | 0:5cd6077e0a8e | 203 | return ; |
hkjung | 0:5cd6077e0a8e | 204 | } |
hkjung | 0:5cd6077e0a8e | 205 | } |
hkjung | 0:5cd6077e0a8e | 206 | } |
hkjung | 0:5cd6077e0a8e | 207 | |
hkjung | 0:5cd6077e0a8e | 208 | int8_t setEchoStatus_BG96(bool onoff) |
hkjung | 0:5cd6077e0a8e | 209 | { |
hkjung | 0:5cd6077e0a8e | 210 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 211 | char _buf[10]; |
hkjung | 0:5cd6077e0a8e | 212 | |
hkjung | 0:5cd6077e0a8e | 213 | sprintf((char *)_buf, "ATE%d", onoff); |
hkjung | 0:5cd6077e0a8e | 214 | |
hkjung | 0:5cd6077e0a8e | 215 | if(_parser->send(_buf) && _parser->recv("OK")) { |
hkjung | 0:5cd6077e0a8e | 216 | devlog("Turn Echo %s success\r\n", onoff?"ON":"OFF"); |
hkjung | 0:5cd6077e0a8e | 217 | ret = RET_OK; |
hkjung | 0:5cd6077e0a8e | 218 | } else { |
hkjung | 0:5cd6077e0a8e | 219 | devlog("Turn Echo %s failed\r\n", onoff?"ON":"OFF"); |
hkjung | 0:5cd6077e0a8e | 220 | } |
hkjung | 0:5cd6077e0a8e | 221 | return ret; |
hkjung | 0:5cd6077e0a8e | 222 | } |
hkjung | 0:5cd6077e0a8e | 223 | |
hkjung | 0:5cd6077e0a8e | 224 | int8_t getUsimStatus_BG96(void) |
hkjung | 0:5cd6077e0a8e | 225 | { |
hkjung | 0:5cd6077e0a8e | 226 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 227 | |
hkjung | 0:5cd6077e0a8e | 228 | _parser->send("AT+CPIN?"); |
hkjung | 0:5cd6077e0a8e | 229 | if(_parser->recv("+CPIN: READY") && _parser->recv("OK")) { |
hkjung | 0:5cd6077e0a8e | 230 | devlog("USIM Status: READY\r\n"); |
hkjung | 0:5cd6077e0a8e | 231 | ret = RET_OK; |
hkjung | 0:5cd6077e0a8e | 232 | } else { |
hkjung | 0:5cd6077e0a8e | 233 | devlog("Retrieving USIM Status failed\r\n"); |
hkjung | 0:5cd6077e0a8e | 234 | } |
hkjung | 0:5cd6077e0a8e | 235 | return ret; |
hkjung | 0:5cd6077e0a8e | 236 | } |
hkjung | 0:5cd6077e0a8e | 237 | |
hkjung | 0:5cd6077e0a8e | 238 | int8_t getNetworkStatus_BG96(void) |
hkjung | 0:5cd6077e0a8e | 239 | { |
hkjung | 0:5cd6077e0a8e | 240 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 241 | |
hkjung | 0:5cd6077e0a8e | 242 | if(_parser->send("AT+QCDS") && _parser->recv("+QCDS: \"SRV\"") && _parser->recv("OK")) { |
hkjung | 0:5cd6077e0a8e | 243 | devlog("Network Status: attached\r\n"); |
hkjung | 0:5cd6077e0a8e | 244 | ret = RET_OK; |
hkjung | 0:5cd6077e0a8e | 245 | } else if (_parser->send("AT+QCDS") && _parser->recv("+QCDS: \"LIMITED\"") && _parser->recv("OK")) { |
hkjung | 0:5cd6077e0a8e | 246 | devlog("Network Status: limited\r\n"); |
hkjung | 0:5cd6077e0a8e | 247 | ret = RET_OK; |
hkjung | 0:5cd6077e0a8e | 248 | } else { |
hkjung | 0:5cd6077e0a8e | 249 | devlog("Network Status: Error\r\n"); |
hkjung | 0:5cd6077e0a8e | 250 | } |
hkjung | 0:5cd6077e0a8e | 251 | return ret; |
hkjung | 0:5cd6077e0a8e | 252 | } |
hkjung | 0:5cd6077e0a8e | 253 | |
hkjung | 0:5cd6077e0a8e | 254 | int8_t checknSetApn_BG96(const char * apn) // Configure Parameters of a TCP/IP Context |
hkjung | 0:5cd6077e0a8e | 255 | { |
hkjung | 0:5cd6077e0a8e | 256 | char resp_str[100]; |
hkjung | 0:5cd6077e0a8e | 257 | |
hkjung | 0:5cd6077e0a8e | 258 | uint16_t i = 0; |
hkjung | 0:5cd6077e0a8e | 259 | char * search_pt; |
hkjung | 0:5cd6077e0a8e | 260 | |
hkjung | 0:5cd6077e0a8e | 261 | memset(resp_str, 0, sizeof(resp_str)); |
hkjung | 0:5cd6077e0a8e | 262 | |
hkjung | 0:5cd6077e0a8e | 263 | devlog("Checking APN...\r\n"); |
hkjung | 0:5cd6077e0a8e | 264 | |
hkjung | 0:5cd6077e0a8e | 265 | _parser->send("AT+QICSGP=1"); |
hkjung | 0:5cd6077e0a8e | 266 | |
hkjung | 0:5cd6077e0a8e | 267 | while(1) |
hkjung | 0:5cd6077e0a8e | 268 | { |
hkjung | 0:5cd6077e0a8e | 269 | _parser->read(&resp_str[i++], 1); |
hkjung | 0:5cd6077e0a8e | 270 | search_pt = strstr(resp_str, "OK\r\n"); |
hkjung | 0:5cd6077e0a8e | 271 | if (search_pt != 0) |
hkjung | 0:5cd6077e0a8e | 272 | { |
hkjung | 0:5cd6077e0a8e | 273 | break; |
hkjung | 0:5cd6077e0a8e | 274 | } |
hkjung | 0:5cd6077e0a8e | 275 | } |
hkjung | 0:5cd6077e0a8e | 276 | |
hkjung | 0:5cd6077e0a8e | 277 | search_pt = strstr(resp_str, apn); |
hkjung | 0:5cd6077e0a8e | 278 | if (search_pt == 0) |
hkjung | 0:5cd6077e0a8e | 279 | { |
hkjung | 0:5cd6077e0a8e | 280 | devlog("Mismatched APN: %s\r\n", resp_str); |
hkjung | 0:5cd6077e0a8e | 281 | devlog("Storing APN %s...\r\n", apn); |
hkjung | 0:5cd6077e0a8e | 282 | if(!(_parser->send("AT+QICSGP=1,%d,\"%s\",\"\",\"\",0", BG96_APN_PROTOCOL, apn) && _parser->recv("OK"))) |
hkjung | 0:5cd6077e0a8e | 283 | { |
hkjung | 0:5cd6077e0a8e | 284 | return RET_NOK; // failed |
hkjung | 0:5cd6077e0a8e | 285 | } |
hkjung | 0:5cd6077e0a8e | 286 | } |
hkjung | 1:8b209e861bb1 | 287 | devlog("APN Check Done\r\n"); |
hkjung | 1:8b209e861bb1 | 288 | |
hkjung | 0:5cd6077e0a8e | 289 | return RET_OK; |
hkjung | 0:5cd6077e0a8e | 290 | } |
hkjung | 0:5cd6077e0a8e | 291 | |
hkjung | 0:5cd6077e0a8e | 292 | int8_t getFirmwareVersion_BG96(char * version) |
hkjung | 0:5cd6077e0a8e | 293 | { |
hkjung | 0:5cd6077e0a8e | 294 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 295 | |
hkjung | 0:5cd6077e0a8e | 296 | if(_parser->send("AT+QGMR") && _parser->recv("%s\n", version) && _parser->recv("OK")) |
hkjung | 0:5cd6077e0a8e | 297 | { |
hkjung | 0:5cd6077e0a8e | 298 | ret = RET_OK; |
hkjung | 0:5cd6077e0a8e | 299 | } |
hkjung | 0:5cd6077e0a8e | 300 | return ret; |
hkjung | 0:5cd6077e0a8e | 301 | } |
hkjung | 0:5cd6077e0a8e | 302 | |
hkjung | 0:5cd6077e0a8e | 303 | int8_t getImeiNumber_BG96(char * imei) |
hkjung | 0:5cd6077e0a8e | 304 | { |
hkjung | 0:5cd6077e0a8e | 305 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 306 | |
hkjung | 0:5cd6077e0a8e | 307 | if(_parser->send("AT+CGSN") && _parser->recv("%s\n", imei) && _parser->recv("OK")) |
hkjung | 0:5cd6077e0a8e | 308 | { |
hkjung | 0:5cd6077e0a8e | 309 | ret = RET_OK; |
hkjung | 0:5cd6077e0a8e | 310 | } |
hkjung | 0:5cd6077e0a8e | 311 | return ret; |
hkjung | 0:5cd6077e0a8e | 312 | } |
hkjung | 0:5cd6077e0a8e | 313 | |
hkjung | 1:8b209e861bb1 | 314 | // ---------------------------------------------------------------- |
hkjung | 1:8b209e861bb1 | 315 | // Functions: Cat.M1 DNS |
hkjung | 1:8b209e861bb1 | 316 | // ---------------------------------------------------------------- |
hkjung | 1:8b209e861bb1 | 317 | |
hkjung | 0:5cd6077e0a8e | 318 | int8_t getIpAddressByName_BG96(const char * name, char * ipstr) |
hkjung | 0:5cd6077e0a8e | 319 | { |
hkjung | 0:5cd6077e0a8e | 320 | char buf2[50]; |
hkjung | 0:5cd6077e0a8e | 321 | bool ok; |
hkjung | 0:5cd6077e0a8e | 322 | int err, ipcount, dnsttl; |
hkjung | 0:5cd6077e0a8e | 323 | |
hkjung | 0:5cd6077e0a8e | 324 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 325 | |
hkjung | 0:5cd6077e0a8e | 326 | ok = ( _parser->send("AT+QIDNSGIP=1,\"%s\"", name) |
hkjung | 0:5cd6077e0a8e | 327 | && _parser->recv("OK") |
hkjung | 0:5cd6077e0a8e | 328 | && _parser->recv("+QIURC: \"dnsgip\",%d,%d,%d", &err, &ipcount, &dnsttl) |
hkjung | 0:5cd6077e0a8e | 329 | && err==0 |
hkjung | 0:5cd6077e0a8e | 330 | && ipcount > 0 |
hkjung | 0:5cd6077e0a8e | 331 | ); |
hkjung | 0:5cd6077e0a8e | 332 | |
hkjung | 0:5cd6077e0a8e | 333 | if( ok ) { |
hkjung | 0:5cd6077e0a8e | 334 | _parser->recv("+QIURC: \"dnsgip\",\"%[^\"]\"", ipstr); //use the first DNS value |
hkjung | 0:5cd6077e0a8e | 335 | for( int i=0; i<ipcount-1; i++ ) |
hkjung | 0:5cd6077e0a8e | 336 | _parser->recv("+QIURC: \"dnsgip\",\"%[^\"]\"", buf2); //and discrard the rest if >1 |
hkjung | 0:5cd6077e0a8e | 337 | |
hkjung | 0:5cd6077e0a8e | 338 | ret = RET_OK; |
hkjung | 0:5cd6077e0a8e | 339 | } |
hkjung | 0:5cd6077e0a8e | 340 | return ret; |
hkjung | 0:5cd6077e0a8e | 341 | } |
hkjung | 0:5cd6077e0a8e | 342 | |
hkjung | 1:8b209e861bb1 | 343 | // ---------------------------------------------------------------- |
hkjung | 1:8b209e861bb1 | 344 | // Functions: Cat.M1 PDP context activate / deactivate |
hkjung | 1:8b209e861bb1 | 345 | // ---------------------------------------------------------------- |
hkjung | 0:5cd6077e0a8e | 346 | |
hkjung | 0:5cd6077e0a8e | 347 | int8_t setContextActivate_BG96(void) // Activate a PDP Context |
hkjung | 0:5cd6077e0a8e | 348 | { |
hkjung | 0:5cd6077e0a8e | 349 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 350 | |
hkjung | 0:5cd6077e0a8e | 351 | _parser->send("AT+QIACT=1"); |
hkjung | 0:5cd6077e0a8e | 352 | if(_parser->recv("OK")) { |
hkjung | 0:5cd6077e0a8e | 353 | devlog("Activate a PDP Context\r\n"); |
hkjung | 0:5cd6077e0a8e | 354 | ret = RET_OK; |
hkjung | 0:5cd6077e0a8e | 355 | } else { |
hkjung | 0:5cd6077e0a8e | 356 | devlog("PDP Context Activation failed\r\n"); |
hkjung | 1:8b209e861bb1 | 357 | } |
hkjung | 0:5cd6077e0a8e | 358 | return ret; |
hkjung | 0:5cd6077e0a8e | 359 | } |
hkjung | 0:5cd6077e0a8e | 360 | |
hkjung | 0:5cd6077e0a8e | 361 | int8_t setContextDeactivate_BG96(void) // Deactivate a PDP Context |
hkjung | 0:5cd6077e0a8e | 362 | { |
hkjung | 0:5cd6077e0a8e | 363 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 364 | |
hkjung | 0:5cd6077e0a8e | 365 | _parser->send("AT+QIDEACT=1"); |
hkjung | 0:5cd6077e0a8e | 366 | if(_parser->recv("OK")) { |
hkjung | 0:5cd6077e0a8e | 367 | devlog("Deactivate a PDP Context\r\n"); |
hkjung | 0:5cd6077e0a8e | 368 | ret = RET_OK; |
hkjung | 0:5cd6077e0a8e | 369 | } else { |
hkjung | 0:5cd6077e0a8e | 370 | devlog("PDP Context Deactivation failed\r\n"); |
hkjung | 0:5cd6077e0a8e | 371 | } |
hkjung | 0:5cd6077e0a8e | 372 | return ret; |
hkjung | 0:5cd6077e0a8e | 373 | } |
hkjung | 0:5cd6077e0a8e | 374 | |
hkjung | 1:8b209e861bb1 | 375 | |
hkjung | 1:8b209e861bb1 | 376 | int8_t getIpAddress_BG96(char * ipstr) // IPv4 or IPv6 |
hkjung | 0:5cd6077e0a8e | 377 | { |
hkjung | 1:8b209e861bb1 | 378 | int8_t ret = RET_NOK; |
hkjung | 1:8b209e861bb1 | 379 | int id, state, type; // not used |
hkjung | 0:5cd6077e0a8e | 380 | |
hkjung | 1:8b209e861bb1 | 381 | _parser->send("AT+QIACT?"); |
hkjung | 1:8b209e861bb1 | 382 | if(_parser->recv("+QIACT: %d,%d,%d,\"%[^\"]\"", &id, &state, &type, ipstr) |
hkjung | 1:8b209e861bb1 | 383 | && _parser->recv("OK")) { |
hkjung | 1:8b209e861bb1 | 384 | ret = RET_OK; |
hkjung | 1:8b209e861bb1 | 385 | } |
hkjung | 0:5cd6077e0a8e | 386 | return ret; |
hkjung | 0:5cd6077e0a8e | 387 | } |
hkjung | 0:5cd6077e0a8e | 388 | |
hkjung | 1:8b209e861bb1 | 389 | // ---------------------------------------------------------------- |
hkjung | 1:8b209e861bb1 | 390 | // Functions: Cat.M1 Ping test |
hkjung | 1:8b209e861bb1 | 391 | // ---------------------------------------------------------------- |
hkjung | 1:8b209e861bb1 | 392 | |
hkjung | 1:8b209e861bb1 | 393 | void printPingToHost_BG96(char * host, int timeout, int pingnum) |
hkjung | 1:8b209e861bb1 | 394 | { |
hkjung | 1:8b209e861bb1 | 395 | int i; |
hkjung | 1:8b209e861bb1 | 396 | char resp_str[50] = {0, }; |
hkjung | 1:8b209e861bb1 | 397 | |
hkjung | 1:8b209e861bb1 | 398 | if((timeout < 1) || (timeout > 255)) { |
hkjung | 1:8b209e861bb1 | 399 | devlog("Ping timeout range is 1-255, and the default value is 4 (unit: sec)\r\n"); |
hkjung | 1:8b209e861bb1 | 400 | return; |
hkjung | 1:8b209e861bb1 | 401 | } |
hkjung | 0:5cd6077e0a8e | 402 | |
hkjung | 1:8b209e861bb1 | 403 | if((pingnum < 1) || (pingnum > 10)) { |
hkjung | 1:8b209e861bb1 | 404 | devlog("The maximum number of sending Ping request range is 1-10, and the default value is 4\r\n"); |
hkjung | 1:8b209e861bb1 | 405 | return; |
hkjung | 1:8b209e861bb1 | 406 | } |
hkjung | 1:8b209e861bb1 | 407 | |
hkjung | 1:8b209e861bb1 | 408 | _parser->set_timeout((1000 * timeout) + 2000); |
hkjung | 0:5cd6077e0a8e | 409 | |
hkjung | 1:8b209e861bb1 | 410 | if(_parser->send("AT+QPING=%d,\"%s\",%d,%d", 1, host, timeout, pingnum) && _parser->recv("OK")) { |
hkjung | 1:8b209e861bb1 | 411 | for(i = 0; i < (pingnum); i++) { |
hkjung | 1:8b209e861bb1 | 412 | _parser->recv("+QPING: %s\n", resp_str); |
hkjung | 1:8b209e861bb1 | 413 | devlog("%s: %s\r\n", host, resp_str); |
hkjung | 1:8b209e861bb1 | 414 | } |
hkjung | 1:8b209e861bb1 | 415 | |
hkjung | 1:8b209e861bb1 | 416 | } |
hkjung | 0:5cd6077e0a8e | 417 | |
hkjung | 0:5cd6077e0a8e | 418 | _parser->set_timeout(BG96_DEFAULT_TIMEOUT); |
hkjung | 1:8b209e861bb1 | 419 | _parser->flush(); |
hkjung | 0:5cd6077e0a8e | 420 | } |